项目中本来应该是一个简单的label就可以实现的东西,产品非要给文本加个类似富文本的效果。
如果文本固定也就算了,偏偏某些字还是不确定的,后台给返回了一串html。
于是网上调研一番,发现UILabel是可以加载html的。具体实现代码如下:
//取出htmlStr
NSString * htmlStr = [NSString stringWithFormat:@"%@",dic[@"shareDesc"]];
//转成带属性的字符串
NSAttributedString * attStr = [[NSAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
//赋值
self.desc.attributedText = attStr;
对就是这样简单。