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

2018-11-06  本文已影响9人  朴子hp


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:@"<span\">%@</span>",html] dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

return attrStr;

}

上一篇下一篇

猜你喜欢

热点阅读