网络解析后 有一个HTML字符串 需要在label显示(一般是要求显示的内容有颜色 字体 背景颜色的变化) 如果不做处理 显示的就是HTML代码内容 处理方法如下
已知网络请求得到一个HTML 字符串 self.HTMLstring
1.富文本显示 不考虑高度自适应
NSMutableAttributedString *str= [[NSMutableAttributedString alloc] initWithData:[self.HTMLstring dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
[str addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:17.0] range:NSMakeRange(0, str.length)];
synthesizeCell.nameLabel.attributedText = str;
2.富文本label的高度自适应
NSMutableAttributedString *str= [[NSMutableAttributedString alloc]
initWithData:[string dataUsingEncoding:NSUnicodeStringEncoding]
options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
documentAttributes:nil
error:nil];
[str addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:kScaleW(16) weight:UIFontWeightSemibold]
range:NSMakeRange(0, str.length)];
CGRect rect = [self.contentLab.attributedText boundingRectWithSize:CGSizeMake(self.contentLab.width, 0)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
context:nil];
self.contentLab.attributedText = str;
[self.contentLab mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(rect.size.height);
}];
原文链接:https://blog.csdn.net/flg1554112450/article/details/53032616