UILabel attributedText html 图片自适应

  • 时间:
  • 浏览:5

-(void)resetDetailHtml{

//图片自适应宽高,只限制图片的最大显示淬硬层 ,可是就能做到自适应

NSString newString =[NSString stringWithFormat:@"<html>"

"<head>"

"</style>"

"<style>
{margin:3px 0px 3px 0px;padding:0 ;max-width:%f;}</style>"

"</head>"

"<body>%@</body>"

"</html>",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);

}