ios UIWebView截获html并修改便签内容
来源:互联网 发布:大数据应用网站 编辑:程序博客网 时间:2024/05/23 10:00
需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果;
方法:通过js截获UIWebView中的html,然后修改html标签内容;
实例代码:
服务器端html
这样显示的结果网页的最小宽度会是device-width;但有时候不需要这个宽度,就需要修改width=device-width为width=myWidth;
客户端代码
这样问题就可以解决了
新增代码:
其他html属性重载和此方法类似;
参考网址:
(stringByEvaluatingJavaScriptFromString的使用方法)http://www.uml.org.cn/mobiledev/201108181.asp
( iphone 获取UIWebView内Html方法)http://blog.csdn.net/diyagoanyhacker/article/details/6564897
(IOS UIWebView引用外部CSS样式)http://hi.baidu.com/jwq359699768/item/780879e5c98bfb3e4ddcaf22
方法:通过js截获UIWebView中的html,然后修改html标签内容;
实例代码:
服务器端html
- <html><head>
- <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>网曝四川省一考场时钟慢半小时 老师称这就是命</title></head<body>网曝四川省一考场时钟慢半小时 老师称这就是命</body></html>
这样显示的结果网页的最小宽度会是device-width;但有时候不需要这个宽度,就需要修改width=device-width为width=myWidth;
客户端代码
- - (void)webViewDidFinishLoad:(UIWebView *)webView
- {
- //修改服务器页面的meta的值
- 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];
- }
这样问题就可以解决了
新增代码:
- //给网页增加utf-8编码
- [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);"];
- //给网页增加css样式
- [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);"];
- //拦截网页图片 并修改图片大小
- [webView stringByEvaluatingJavaScriptFromString:
- @"var script = document.createElement('script');"
- "script.type = 'text/javascript';"
- "script.text = \"function ResizeImages() { "
- "var myimg,oldwidth;"
- "var maxwidth=380;" //缩放系数
- "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();"];
其他html属性重载和此方法类似;
参考网址:
(stringByEvaluatingJavaScriptFromString的使用方法)http://www.uml.org.cn/mobiledev/201108181.asp
( iphone 获取UIWebView内Html方法)http://blog.csdn.net/diyagoanyhacker/article/details/6564897
(IOS UIWebView引用外部CSS样式)http://hi.baidu.com/jwq359699768/item/780879e5c98bfb3e4ddcaf22
http://blog.csdn.net/xdonx/article/details/6973521
转载自:http://blog.csdn.net/matrixhero/article/details/8203208
0 0
- ios UIWebView截获html并修改便签内容
- ios UIWebView截获html并修改便签内容
- ios UIWebView截获html并修改便签内容
- ios UIWebView截获html并修改便签内容
- IOS UIWebView截获html并修改便签内容,宽度自适应
- ios UIWebView截获html并修改便签内容
- IOS UIWebView截获html并修改便签内容,宽度自适应
- IOS UIWebView截获html并修改便签内容,宽度自适应
- ios UIWebView截获html并修改便签内容
- ios UIWebView截获html并修改便签内容
- ios UIWebView截获html并修改便签内容
- ios UIWebView截获html并修改便签内容
- IOS UIWebView截获html并修改便签内容,宽度自适应
- ios UIWebView截获html并修改便签内容
- ios UIWebView截获html并修改便签内容
- UIWebView截获html并修改便签内容
- UIWebView自适应宽度 截获html并修改便签内容
- IOS开发之——UIWebView截获html并修改便签内容
- 看到了很不错的css3 animation的教程
- STK 《Connect命令参考手册》首发
- Phonegap2.9.0本地打包html成ios应用
- Android 自定义控件之界面标题导航及控件打包共享
- PhoneGap2.9.0本地将html打包成Android应用
- ios UIWebView截获html并修改便签内容
- 关于hibernate调用存储过程
- HDU1588-Gauss Fibonacci(矩阵快速幂+等比数列二分求和)
- 浏览器的工作原理(一)
- 删除当前用户所有表
- Codeforces 67A. Partial Teacher
- PhoneGap2.9.0本地将html打包成Android应用
- chapter5
- 只有一个broker的例子