一、UIWebView 中的 方法可以直接加载
stringByEvaluatingJavaScriptFromString:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[webView stringByEvaluatingJavaScriptFromString:
@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth = 1000.0;" // UIWebView中显示的图片宽度
"for(i= 1;i <document.images.length;i++){" // 之所以i= 1,因为我的0号位是头像,其他都是需要放大的,适应屏幕的
"myimg = document.images[i];"
"oldwidth = myimg.width;"
"myimg.width = maxwidth;"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);
ResizeImages();
"];
}
二、WKWebVIew 也就是方法不一样
[evaluateJavaScript:completionHandler: ]方法,JS部分和上面的一样。
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
[ webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '300%'" completionHandler:nil];
[ webView evaluateJavaScript:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth = 1000.0;" // UIWebView中显示的图片宽度
"for(i=1;i <document.images.length;i++){"
"myimg = document.images[i];"
"oldwidth = myimg.width;"
"myimg.width = maxwidth;"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);ResizeImages();" completionHandler:nil];
}
网友评论