让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;
本文详细介绍了如何使UITextView自适应高度,并展示了如何将HTML内容以美观的方式显示在iOS应用中。同时,还分享了如何在webView中实现自适应高度,确保用户体验不受屏幕大小限制。
1809





