IOS UIWebView intercepts html and modifies the tab content, adaptive width, and iosuiwebview
Requirement: After the hybrid app UIWebView opens html, UIWebView has a left and right scroll bar to remove the left and right scrolling effect. Method: intercept html in UIWebView through js, and then modify the html Tag content. instance code: server-side html
Java code
- <Html>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
- <Meta name = "viewport" content = "width = device-width, initial-scale = 1.0, minimum-scale = 1.0, maximum-scale = 1.0, user-scalable = no ">
- <Title> Internet exposure Sichuan Province 1 test room clock slow for half an hour teacher said this is life </title>
In this way, the minimum width of the displayed webpage is device-width, but sometimes you do not need this width, You need to modify width = device-width to width = myWidth;
Client code
Java code
- -(Void) webViewDidFinishLoad :( UIWebView *) webView
- {
- // Modify the meta value of the server page
- NSString * meta = [NSString stringWithFormat: @ "document. getElementsByName (\ "viewport \") [0]. content = \ "width = % f, initial-scale = 1.0, minimum-scale = 1.0, maximum-scale = 1.0, user-scalable = no \" ", webView. frame. size. width];
- [WebView stringByEvaluatingJavaScriptFromString: meta];
- }
In this way, the problem can be solved.
Add code:
Java code
- // Add UTF-8 encoding to the webpage
- [WebView stringByEvaluatingJavaScriptFromString:
- @ "Var tagHead upload document.doc umentElement. firstChild ;"
- "Var tagMeta = document. createElement (\" meta \");"
- "TagMeta. setAttribute (\" http-equiv \ ", \" Content-Type \");"
- "Tagmeta. setAttribute (\" content \ ", \" text/html; charset = UTF-8 \");"
- "Var tagHeadAdd = tagHead. appendChild (tagMeta);"];
Java code
- // Add a css style to the webpage
- [WebView stringByEvaluatingJavaScriptFromString:
- @ "Var tagHead upload document.doc umentElement. firstChild ;"
- "Var tagStyle = document. createElement (\" style \");"
- "TagStyle. setAttribute (\" type \ ", \" text/css \");"
- "TagStyle. appendChild (document. createTextNode (\" BODY {padding: 20pt 15pt }\"));"
- "Var tagHeadAdd = tagHead. appendChild (tagStyle);"];
Java code
- // Intercept the webpage image and modify the image size
- [WebView stringByEvaluatingJavaScriptFromString:
- @ "Var script = document. createElement ('script ');"
- "Script. type = 'text/javascript ';"
- "Script. text = \" function ResizeImages (){"
- "Var myimg, oldwidth ;"
- "Var maxwidth = 380;" // Scaling Factor
- "For (I = 0; I <document. images. length; I ++ ){"
- "Myimg = document. images [I];"
- "If (myimg. width> maxwidth ){"
- "Oldwidth = myimg. width ;"
- "Myimg. width = maxwidth ;"
- "Myimg. height = myimg. height * (maxwidth/oldwidth );"
- "}"
- "}"
- "}\";"
- "Document. getElementsByTagName ('head') [0]. appendChild (script);"];
- [WebView stringByEvaluatingJavaScriptFromString: @ "ResizeImages ();"];
Other html attribute overloading methods are similar to this method;
Reference URL:
(How to Use stringByEvaluatingJavaScriptFromString) http://www.uml.org.cn/mobiledev/201108181.asp
(Iphone get UIWebView Html method in) http://blog.csdn.net/diyagoanyhacker/article/details/6564897
(IOS UIWebView reference external CSS style) http://hi.baidu.com/jwq359699768/item/780879e5c98bfb3e4ddcaf22
I use andrwebview to load html document self-adaptation
Percentage layout. It seems that webkit mobile Browsers Do not support table code.
Can ios uiwebview load json data?
Yes, but you need to convert json into your data structure. xml is also the same. It is impossible to load a json or xml data directly.