最后使用的是UIWebView,解决了两个问题
1:开始使用的WKWebView,发现WKWebView加载的html字符串文字要比正常的小很多,找了几个办法都没有解决,因此只能换成了UIWebView
2:换了后这次文字倒是正常了,但是图片又不能完整显示在WebView里,后来找到JS方法在WebView加载完后调用,才解决
这里贴一下主要代码
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *js=@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth = %f;"
"for(i=0;i <document.images.length;i++){"
"myimg = document.images[i];"
"if(myimg.width > maxwidth){"
"oldwidth = myimg.width;"
"myimg.width = %f;"
"}"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);";
js = [NSString stringWithFormat:js,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.width-15];
[webView stringByEvaluatingJavaScriptFromString:js];
[webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
NSInteger width = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth"] integerValue];
CGFloat bili = width/ScreenWIDTH;
NSInteger height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] integerValue];
[_PolicyWebView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(height/bili);//更新高度
}];
}