- 需求1
有的html字符串中無(wú)法顯示
\
泰演,而是顯示為"
呻拌。這時(shí)需要將"
等類(lèi)似的字符轉(zhuǎn)化為HTML中的\
等。還有<
和>
等本應(yīng)該代表標(biāo)簽符號(hào)的字符睦焕,也需要換成<
和>
等藐握。
- (NSString *)htmlEntityDecode:(NSString *)string
{
string = [string stringByReplacingOccurrencesOfString:@""" withString:@"\""];
string = [string stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
string = [string stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
string = [string stringByReplacingOccurrencesOfString:@">" withString:@">"];
string = [string stringByReplacingOccurrencesOfString:@"&" withString:@"&"]; // Do this last so that, e.g. @"&lt;" goes to @"<" not @"<"
return string;
}
- 需求2
將HTML字符串轉(zhuǎn)化為NSAttributedString富文本字符串靴拱,并用UILabel,UITextview的attributedText屬性加載富文本猾普。
- (NSAttributedString *)attributedStringWithHTMLString:(NSString *)htmlString
{
NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) };
NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
return [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
}
- 需求3
忽略所有格式袜炕,取出純文本內(nèi)容,即去掉 HTML 字符串中的所有標(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;
}