最近项目里面遇见了 HTML 字符串,整理如下:

在 iOS 中通常加载 HTML 字符串有两种方式
- 通过 UILabel 加载富文本的方法加载 HTML 字符串
- 通过 WebView 加载 HTML 字符串
- (void)viewDidLoad {
[super viewDidLoad];
1.UILabel 加载 HTML 字符串
NSString * str1 = @"<div>Google(中文名:谷歌),是一家美国的跨国科技企业。</div><div>Google由当时在斯坦福大学攻读理工博士的拉里·佩奇和谢尔盖·布卢姆共同创建,因此两人也被称为“Google Guys”。</div><div>1998年9月4日,Google以私营公司的形式创立,设计并管理一个互联网搜索引擎“Google搜索”。</div>";
NSString * str2 = @"<p><br></p>";
NSString * str3 = @"<p>qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq</p>";
//1.将字符串转化为标准HTML字符串
str1 = [self htmlEntityDecode:str1];
//2.将HTML字符串转换为attributeString
NSAttributedString * attributeStr = [self attributedStringWithHTMLString:str1];
//3.使用label加载html字符串
self.label.attributedText = attributeStr;
2.UIWebView 加载HTML字符串
UIWebView * webView = [[UIWebView alloc]initWithFrame:CGRectMake(20, 300, self.view.frame.size.width - 40, 400)];
[webView loadHTMLString:str1 baseURL:nil];
[self.view addSubview:webView];
self.webView = webView;
}
//将 < 等类似的字符转化为HTML中的“<”等
- (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;
}
//将HTML字符串转化为NSAttributedString富文本字符串
- (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];
}
//去掉 HTML 字符串中的标签
- (NSString *)filterHTML:(NSString *)html
{
NSScanner * scanner = [NSScanner scannerWithString:html];
NSString * text = nil;
while([scanner isAtEnd]==NO)
{
//找到标签的起始位置
[scanner scanUpToString:@"<" intoString:nil];
//找到标签的结束位置
[scanner scanUpToString:@">" intoString:&text];
//替换字符
html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
}
// NSString * regEx = @"<([^>]*)>";
// html = [html stringByReplacingOccurrencesOfString:regEx withString:@""];
return html;
}
** 注意 **
- 此处的字符串不是标准的标签的HTML字符串,所以我们首先要调用- (NSString *)htmlEntityDecode:(NSString *)string 方法将字符串转换成标准的HTML字符串,这样才可以进行HTML字符串的加载
- 实例的第二个字符串中的内容为空,当用 Label 加载的时候只是两行空白数据,此时可以调用- (NSString *)filterHTML:(NSString *)html 方法,去掉标签,将HTML字符串转换为常用的字符串样式
网友评论
<p> 置身佛之国度,聆听神祗的声音生活在西双版纳的人们多信奉佛教,村村寨寨几乎都有佛寺和造型如纺锤的佛塔。据统计,西双版纳州内有佛寺550座,堪称全国之最。佛寺、佛塔是西双版纳旅游不可错过的景观。</p>
<h3><span style="color: rgba(255, 0, 0, 1.0)"><strong>欣赏原生态民族歌舞</strong></span></h3>
<p> 西双版纳生活的少数民族,都能歌善舞,除了傣族的孔雀舞之外,脚鼓舞、章哈调也是具有代表性的傣族歌舞。另外,爱伲族的咚巴叉、竹筒舞,布朗族的弹唱和基诺族的大鼓舞也各具特色,来到这里,一定要欣赏这些原生态的民族舞蹈。</p>
<h3><span style="color: rgba(255, 0, 0, 1.0)"><strong>逛版纳夜市,品正宗傣味美食</strong></span></h3>
<p> 西双版纳的美食尽在夜市,当夜幕降临,边品尝风味佳肴,边观赏景洪城夜景,五颜六色的灯光会让人流连忘返。如果有勇气,不妨吃虫宴,除了蚱蜢、蝎子,还有蠕动的竹虫、蜜蜂幼虫等,绝对会让你拥有一次难忘的记忆。</p>
<h3><span style="color: rgba(255, 0, 0, 1.0)"><strong>水中狂欢,参加傣族泼水节</strong></span></h3>
<p> 4月,菩提树吐绿,凤凰花怒放,地处西南边陲的美丽“绿洲”西双版纳迎来了狂欢的泼水节。</p>
<p> 人们相约赶摆、沐佛、放生,走上街头,相互用水尽情地泼洒幸福,欢乐、祥和的氛围会让你的情绪瞬间高涨,也不自禁的融入其中。</p>
像这种的要怎么展示呢?我用了你缩写的但是展示不出来