(1)用UIWebview加载
// 自适应尺寸大小
- (NSString *)autoWebAutoImageSize:(NSString *)html{
//搜索标签文本中的
标签
NSString * regExpStr = @"
";
NSRegularExpression *regex=[NSRegularExpression regularExpressionWithPattern:regExpStr options:NSRegularExpressionCaseInsensitive error:nil];
NSArray *matches=[regex matchesInString:html
options:0
range:NSMakeRange(0, [html length])];
NSMutableArray * mutArray = [NSMutableArray array];
for (NSTextCheckingResult *match in matches) {
NSString* group1 = [html substringWithRange:[match rangeAtIndex:1]];
[mutArray addObject: group1];
}
NSUInteger len = [mutArray count];
for (int i = 0; i < len; ++ i) {
//在img标签内添加style,设置image的宽度,高度
NSString *imageStr = [mutArray[i] stringByAppendingString:@" style=\"width:100%; height:auto;\""];
html = [html stringByReplacingOccurrencesOfString:mutArray[i] withString:imageStr];
}
return html;
}
(2)用UITextView(UILabeL)加载
- (NSAttributedString *)showAttributedToHtml:(NSString *)html withWidth:(float)width{
//替换图片的高度为屏幕的高度
//width为UITextView的宽度
NSString *str = [NSString stringWithFormat:@"%@",width,html];
NSAttributedString *attributeString=[[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
return attributeString;
}