网络解析后 有一个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:[self.HTMLstring dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
[str addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:NSMakeRange(0, str.length)];(字体font是自定
iOS label显示HTML(富文本)代码 (解析)并实现高度自适应
最新推荐文章于 2024-05-03 17:47:14 发布