让UITextView自适应高度:
其中strHTML是html字符串。
//自适应高度
CGRect frame = _textView.frame;
CGSize size = [_textView.text sizeWithFont:_textView.font
constrainedToSize:CGSizeMake(280, 1000)
lineBreakMode:NSLineBreakByTruncatingTail];
frame.size.height = size.height > 1 ? size.height + 20 : 64;
_textView.frame = frame;
显示html内容:
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[strHTML dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
_textView.attributedText = attributedString;
其中strHTML是html字符串。
再来个webView的自适应高度:
CGRect frame = webView.frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;