在做项目的过程中可能有的网络请求下来的是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];