美文网首页
UILabel加载富文本图片大小自适应

UILabel加载富文本图片大小自适应

作者: 倪大头 | 来源:发表于2020-07-14 16:53 被阅读0次

给htmlstring加一个<head>

htmlStr = [NSString stringWithFormat:@"<head><style>img{max-width:%f !important;height:auto}</style></head>%@", UI_SCREEN_WIDTH, htmlStr];
NSMutableAttributedString *htmlStrAttri = [[NSMutableAttributedString alloc]initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
// 行间距(NSParagraphStyleAttributeName)
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:0];
[htmlStrAttri addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [htmlStrAttri length])];
UILabel *htmlLabel = [[UILabel alloc]init];
htmlLabel.attributedText = htmlStrAttri;
htmlLabel.font = [UIFont systemFontOfSize:13];
htmlLabel.textColor = [UIColor colorWithHexString:@"333333"];
htmlLabel.numberOfLines = 0;
htmlLabel.lineBreakMode = NSLineBreakByWordWrapping;
htmlLabel.backgroundColor = UIColor.whiteColor;
[htmlLabel sizeToFit];
[self.view addSubview:htmlLabel];
[htmlLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_offset(10);
    make.top.mas_offset(10);
    make.width.mas_equalTo(UI_SCREEN_WIDTH - 20);
}];

相关文章

网友评论

      本文标题:UILabel加载富文本图片大小自适应

      本文链接:https://www.haomeiwen.com/subject/txefhktx.html