Swift3.0 UIWebView修改html body中的字体、图片大小

上代码!
在代理方法webViewDidFinishLoad里做操作

func webViewDidFinishLoad(_ webView: UIWebView){
let str = "document.body.style.fontSize = '13px';"
webView.stringByEvaluatingJavaScript(from: str)
var imageauto = "var script = document.createElement('script');"
imageauto+="script.type = 'text/javascript';"
imageauto+="script.text = "function ResizeImages() { "
imageauto+="var myimg,oldwidth;"
imageauto+="var maxwidth = %f;"
imageauto+="for(i=0;i imageauto+="myimg = document.images[i];"
imageauto+="if(myimg.width > maxwidth){"
imageauto+="oldwidth = myimg.width;"
imageauto+="myimg.width = %f;"
imageauto+="}"
imageauto+="}"
imageauto+="}";"
imageauto+="document.getElementsByTagName('head')[0].appendChild(script);"
imageauto = String(format: imageauto, UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.width - 15)
webView.stringByEvaluatingJavaScript(from: imageauto)
webView.stringByEvaluatingJavaScript(from: "ResizeImages();")
}

你可能感兴趣的:(Swift3.0 UIWebView修改html body中的字体、图片大小)