在做项目的过程中可能有的网络请求下来的是json数据,但是数据中有一个key对应的value是html字符串,这就需要用UILabel显示html了,用以下代码即可。
//text是html的字符串
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[text dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds];
label.numberOfLines = 0;
[label setAttributedText:attrStr];
[self.view addSubview:label];
本文介绍了一种方法,在iOS项目中将HTML字符串显示到UILabel上,通过使用NSAttributedString来解析HTML并正确显示。
1万+

被折叠的 条评论
为什么被折叠?



