方法一(單純?nèi)コ齢tml標(biāo)簽):
- NSScanner去除標(biāo)簽
-(NSString *)filterHTML:(NSString *)html
{
NSScanner * scanner = [NSScanner scannerWithString:html];
NSString * text = nil;
while([scanner isAtEnd]==NO)
{
//找到標(biāo)簽的起始位置
[scanner scanUpToString:@"<" intoString:nil];
//找到標(biāo)簽的結(jié)束位置
[scanner scanUpToString:@">" intoString:&text];
//替換字符
html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
}
// NSString * regEx = @"<([^>]*)>";
// html = [html stringByReplacingOccurrencesOfString:regEx withString:@""];
return html;
}
參考鏈接:http://www.cnblogs.com/machealking/p/4647012.html
- 正則去除標(biāo)簽
//正則去除標(biāo)簽
-(NSString *)getZZwithString:(NSString *)string{
NSRegularExpression *regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>|\n"
options:0
error:nil];
string=[regularExpretion stringByReplacingMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) withTemplate:@""];
return string;
}
方法二(轉(zhuǎn)化html標(biāo)簽為富文本):
//轉(zhuǎn)化html標(biāo)簽為富文本
+ (NSMutableAttributedString *)praseHtmlStr:(NSString *)htmlStr {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, attributedString.length)];
[attributedString addAttribute:NSForegroundColorAttributeName value:CommonColor(Color333333) range:NSMakeRange(0, attributedString.length)];
return attributedString;
}