关于HTML字符串处理的几种方式


1.去除HTML标签


+ (NSString*)filterHTML:(NSString*)html

{

    NSScanner* scanner = [NSScannerscannerWithString:html];

    NSString* text =nil;

    while([scannerisAtEnd]==NO)

    {

        //找到标签的起始位置

        [scannerscanUpToString:@"<" intoString:nil];

        //找到标签的结束位置

        [scannerscanUpToString:@">"intoString:&text];

        //替换字符

        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];

    }

    return html;

}    

2.标签语言转换富文本

+ (NSAttributedString*)HTMLStrToAttributedString:(NSString*)html;{   

 NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[[NSString stringWithFormat:@"%@",html] dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

return attrStr;

}

你可能感兴趣的:(关于HTML字符串处理的几种方式)