IOS UIWebView intercepts HTML and modifies note content

Source: Internet
Author: User

UIWebView often encountered with the use of JS to deal with the things, and today encountered a search, find this article feel good to cherish a bit.

Original address http://jiapumin.iteye.com/blog/1558345 thank the original author Jiapumin demand: Mixed application UIWebView open HTML, UIWebView have left and right scroll bar, to remove the left and right scrolling effect;
Methods: Using JS to intercept the HTML in UIWebView, and then modify the contents of HTML tags;
Instance code:
Server-side HTML
    1. <meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">
    2. <meta name="viewport" content="Width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale= 1.0, User-scalable=no ">
    3. <title> network exposure to a test room clock slow half hour teacher said this is life </title>

The minimum width of the resulting web page will be device-width, but sometimes it is not necessary to modify the width=device-width to Width=mywidth;
Client 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. }

So the problem can be solved.

New Code:

    1. Add utf-8 encoding to Web pages
    2. [WebView stringbyevaluatingjavascriptfromstring:
    3. @"var taghead =document.documentelement.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);"];

    1. Add CSS styles to a Web page
    2. [WebView stringbyevaluatingjavascriptfromstring:
    3. @"var taghead =document.documentelement.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);"];


  1. Block page images and modify 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;" //Zoom 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 property overloads are similar to this method;
Reference URL:
(How to use stringbyevaluatingjavascriptfromstring) http://www.uml.org.cn/mobiledev/201108181.asp
(iphone gets UIWebView inside HTML method) http://blog.csdn.net/diyagoanyhacker/article/details/6564897
(IOS UIWebView referencing external CSS styles) http://hi.baidu.com/jwq359699768/item/780879e5c98bfb3e4ddcaf22
http://blog.csdn.net/xdonx/article/details/6973521

IOS UIWebView intercepts HTML and modifies note content

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.