在webView中先设置下列HTML代码,让webView能完美适配富文本的内容,之后取到的高度就是准确的高度。
NSString *str = [model.getModel stringByAppendingString: @"<html><head><meta content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0\" name=\"viewport\"><style type=\"text/css\">img{display: inline-block;max-width:100%}</style></head><body></body></html>"];
[self.webView loadHTMLString:str baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]];
获取wkwebView准确高度代码:
//加载完成之后调用
-(void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
self.webView.scrollView.scrollEnabled = NO;
// [webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '200%'" completionHandler:nil];
[webView evaluateJavaScript:@"document.documentElement.scrollHeight"completionHandler:^(id _Nullable result,NSError * _Nullable error){
SSLog(@"scrollHeight高度:%.2f",[result floatValue]);
if (self.webHeight <= 0) {
self.webHeight = [result floatValue];//获取的精准高度
//刷新方法
}
}];
}