UILabel attributedText html 图片自适应

-(void)resetDetailHtml{
//图片自适应宽高,只限制图片的最大显示宽度,这样就能做到自适应
NSString newString =[NSString stringWithFormat:@""
""
""
""
""
"%@"
"",self.view.frame.size.width-30,_detailHtmlData];
NSData *data = [newString dataUsingEncoding:NSUnicodeStringEncoding];
NSDictionary *options = @{
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType
};
NSMutableAttributedString *htmlAttribute = [[NSMutableAttributedString alloc] initWithData:data
options:options
documentAttributes:nil
error:nil];
//设置字体
autoSize6
UIFont baseFont = [UIFont systemFontOfSize:15aSX];
[htmlAttribute addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, htmlAttribute.length)];//设置所有的字体
_detailView.attributedText = htmlAttribute;
CGRect frame = _detailView.frame;
CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
CGSize size = [_detailView sizeThatFits:constraintSize];
if (size.height<=frame.size.height) {
size.height=frame.size.height;
}
_detailView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
}

你可能感兴趣的:(UILabel attributedText html 图片自适应)