Requirements: 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
Java Code Collection Code
<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> 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
Java Code Collection Code
-(void) Webviewdidfinishload: (UIWebView *) WebView
{
To modify the meta value of a 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];
}
So the problem can be solved.
New Code:
Java Code Collection Code
Add utf-8 encoding to Web pages
[WebView stringbyevaluatingjavascriptfromstring:
@ "var taghead =document.documentelement.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 Collection Code
Add CSS styles to a Web page
[WebView stringbyevaluatingjavascriptfromstring:
@ "var taghead =document.documentelement.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 Collection Code
Block page images and modify 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 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