UITextView 加载html文本(图片自适应)
NSString * htmlsTr = @"哦吧你不会比谁都不休的白色修身<p>才能完呢冲破恩微博撇那狗骗你我饿够了呢比你耳边n伪军抹去你问邪恶</p><p><img src=\"http://yk3.gokuai.com/index/thumb?mount_id=1255485&type=jpeg&hash=c36c982f5d047666d7173c3625550152a2d403e6&filehash=ca9ab472b4914bc0b3e7eefc9fa0fa66d75091bf&big=1\" alt=\"Gravatar\" style=\"max-width:100%;display: block; position: relative; margin: auto;\"></p><p>热敷已成为8哦鳄鱼从背后有我饿</p><img src=\"http://yk3.gokuai.com/index/thumb?mount_id=1255485&type=jpeg&hash=565b67349cf2f08d4c373b350355adae46d7199c&filehash=169585a54ecc8311159717a1f8334ba0045a7532&big=1\" alt=\"Gravatar\" style=\"max-width:100%;display: block; position: relative; margin: auto;\"><p><a href=\"http://yk3.gokuai.com/file/hb14k046sx0l6gn2h471nkxm8i9zcau1#\" target=\"_blank\"><img src=\"http://dn-preview.gokuai.com/23/237b4494a9cc56b3c29dc1d34be8340774cf9ce6_1280.jpg\" alt=\"File\" style=\"max-width:100%;\"></a>去年细纹卡马乔全部下次去把握和刺猬比欧巴</p>";
NSString * urlStr1 = [NSString stringWithFormat:@"<html><body style=\"word-wrap:break-word;word-break:break-all;overflow: hidden;\">%@</body></html>",htmlsTr];
[self.webView loadHTMLString:urlStr1 baseURL:nil];
计算高度注意这里是异步的
//页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[webView evaluateJavaScript:@"document.body.offsetHeight;" completionHandler:^(id _Nullable any, NSError * _Nullable error) {
NSString *heightStr = [NSString stringWithFormat:@"%@",any];
NSLog(@"--高度--%@",heightStr);
}];
}
-(WKWebView *)webView
{
if (!_webView) {
NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *wkUController = [[WKUserContentController alloc] init];
[wkUController addUserScript:wkUScript];
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = wkUController;
_webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:wkWebConfig];
_webView.navigationDelegate = self;
_webView.scrollView.bounces = NO;
_webView.scrollView.alwaysBounceVertical = NO;
_webView.scrollView.scrollEnabled = NO;
// [_webView loadHTMLString:<#(nonnull NSString *)#> baseURL:<#(nullable NSURL *)#>]
// [_webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil]; 监听contentsize 变化
[self.bottomView addSubview:_webView];
}
return _webView;
}
效果图