- 字体不能改变:
- NSString * htmlString = @"<html><body> Some html string \n <font size=\"13\" color=\"red\">This is some text!</font> </body></html>";
- NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
- UILabel * myLabel = [[UILabel alloc] initWithFrame:self.view.bounds];
- myLabel.attributedText = attrStr;
- [self.view addSubview:myLabel];
第二种方法:
// 字符串转化 (label字体可以设置)
-(NSString *)filterHTML:(NSString *)html
{
NSScanner * scanner = [NSScannerscannerWithString:html];
NSString * text =nil;
while([scannerisAtEnd]==NO)
{
//找到标签的起始位置
[scanner scanUpToString:@"<"intoString:nil];
//找到标签的结束位置
[scanner scanUpToString:@">"intoString:&text];
//替换字符
html = [html stringByReplacingOccurrencesOfString:[NSStringstringWithFormat:@"%@>",text]withString:@""];
}
// NSString * regEx = @"<([^>]*)>";
// html = [html stringByReplacingOccurrencesOfString:regEx withString:@""];
return html;
}