IOS UIWebView intercepts html and modifies the tab content, adaptive width, and iosuiwebview

Source: Internet
Author: User

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
  1. <Html>
  2. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
  3. <Meta name = "viewport" content = "width = device-width, initial-scale = 1.0, minimum-scale = 1.0, maximum-scale = 1.0, user-scalable = no ">
  4. <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
  1. -(Void) webViewDidFinishLoad :( UIWebView *) webView
  2. {
  3. // Modify the meta value of the server page
  4. 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];
  5. [WebView stringByEvaluatingJavaScriptFromString: meta];
  6. }


In this way, the problem can be solved.

Add code:

Java code
  1. // Add UTF-8 encoding to the webpage
  2. [WebView stringByEvaluatingJavaScriptFromString:
  3. @ "Var tagHead upload document.doc umentElement. firstChild ;"
  4. "Var tagMeta = document. createElement (\" meta \");"
  5. "TagMeta. setAttribute (\" http-equiv \ ", \" Content-Type \");"
  6. "Tagmeta. setAttribute (\" content \ ", \" text/html; charset = UTF-8 \");"
  7. "Var tagHeadAdd = tagHead. appendChild (tagMeta);"];

 

Java code
  1. // Add a css style to the webpage
  2. [WebView stringByEvaluatingJavaScriptFromString:
  3. @ "Var tagHead upload document.doc umentElement. firstChild ;"
  4. "Var tagStyle = document. createElement (\" style \");"
  5. "TagStyle. setAttribute (\" type \ ", \" text/css \");"
  6. "TagStyle. appendChild (document. createTextNode (\" BODY {padding: 20pt 15pt }\"));"
  7. "Var tagHeadAdd = tagHead. appendChild (tagStyle);"];



Java code
  1. // Intercept the webpage image and modify the image size
  2. [WebView stringByEvaluatingJavaScriptFromString:
  3. @ "Var script = document. createElement ('script ');"
  4. "Script. type = 'text/javascript ';"
  5. "Script. text = \" function ResizeImages (){"
  6. "Var myimg, oldwidth ;"
  7. "Var maxwidth = 380;" // Scaling Factor
  8. "For (I = 0; I <document. images. length; I ++ ){"
  9. "Myimg = document. images [I];"
  10. "If (myimg. width> maxwidth ){"
  11. "Oldwidth = myimg. width ;"
  12. "Myimg. width = maxwidth ;"
  13. "Myimg. height = myimg. height * (maxwidth/oldwidth );"
  14. "}"
  15. "}"
  16. "}\";"
  17. "Document. getElementsByTagName ('head') [0]. appendChild (script);"];
  18. [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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.