UIWebview 截获html并修改内容。
需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果;
方法:通过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
UIWebview 截获html并修改内容。的更多相关文章
- ios UIWebView截获html并修改便签内容(转载)
ios UIWebView截获html并修改便签内容 博客分类: iphone开发iphone开发phoneGap uiwebviewstringByEvaluatingJavaScriptFromS ...
- IOS UIWebView截获html并修改便签内容,宽度自适应
需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果: 方法:通过js截获UIWebView中的html,然后修改html标签内容: 实例代码: 服 ...
- ios UIWebView截获html并修改便签内容
需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果: 方法:通过js截获UIWebView中的html,然后修改html标签内容: 实例代码: 服务器端 ...
- POI根据EXCEL模板,修改内容导出新EXCEL (只支持HSSF)
package excelPoiTest; import java.io.File; import java.io.FileInputStream; import java.io.FileOutput ...
- Oracle 中利用闪回查询确定某表在某时间点之后的修改内容,并恢复至该时间点
Oracle 中利用闪回查询确定某表在某时间点之后的修改内容: 1.查看 DELETE 及 UPDATE 操作修改的数据: SQL> SELECT * FROM tab AS OF TIMEST ...
- Example011表单中修改内容
<!-- 实例011表单中修改内容的方法 --> <!DOCTYPE html> <html lang="en"> <head> & ...
- dedecms中{dede:myad name='about'/} 修改内容
网站首页index.htm中调用这个命令,显示一段文字,但是想要修改内容.所以想知道这个命令指定的文件内容在哪里寻找或者指定内容在哪里修改? 匿名 | 浏览 6036 次 发布于2014-02-19 ...
- git教程: 查看文件状态与修改内容
转载:时光机穿梭 我们已经成功地添加并提交了一个readme.txt文件,现在,是时候继续工作了,于是,我们继续修改readme.txt文件,改成如下内容: Git is a distributed ...
- Git 查询某次历史提交的修改内容
在工作时,有时候想查看某次的提交修改了哪些的内容. 我们首先可以git log显示历史的提交列表: 之后我们用git show <commit-hashId> 便可以显示某次提交的修改内容 ...
随机推荐
- 微軟将弃用 System.Data.OracleClient
http://www.cnblogs.com/WizardWu/archive/2010/05/17/1737009.html 微軟将从 .NET 4 以后的版本弃用 System.Data.Orac ...
- ubuntu How do I configure proxies without GUI?
想法: 我的想法是想是一台国内的 ubuntu 云主机可以通过另外一台在国外(新加坡)的服务器 ,来实现可以访问 google ,哈哈,比较好查资料:) 下面的做法 去修改 /etc/environ ...
- Behavioral模式之Interpreter模式
1.意图 给定一个语言,定义它的文法的一种表示.并定义一个解释器,这个解释器使用该表示来解释语言中的句子. 2.别名 无 3.动机 假设一种特定类型的问题发生的频率足够高,那么可能就值得将该问题的各种 ...
- PAT Broken Keyboard (20)
题目描写叙述 On a broken keyboard, some of the keys are worn out. So when you type some sentences, the cha ...
- 用XMLRPC开服务进行server/client通信
本文讲一下怎样用python的xmlrpc开服务,进行server/client的通信. 应用场景:1)需多client訪问应用程序给予应答情况--网页服务. 2)数据极大,希望载入一次.后面仅仅用 ...
- Project Euler:Problem 42 Coded triangle numbers
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...
- css中合理的使用nth-child实现布局
写这篇文章的目的.主要是今天要实现一个布局:li.每行三个,总数不定.仅仅能相邻的li之间须要10px的间距.效果例如以下图: watermark/2/text/aHR0cDovL2Jsb2cuY3N ...
- 【bzoj1082】栅栏[SCOI2005]
显然我们取的肯定是前ans块木板.然后砍的木材也应该是从小到大砍(如果小的木材可以满足条件,就一定不会去动大的木材) 所以两遍排序. 二分答案. 然后对于要取的每块木板,我们搜索它是在第x块木板上砍下 ...
- ios11--UIButton
// // ViewController.m // 02-UIButton(在代码中使用) // #import "ViewController.h" @interface Vie ...
- Protected vs protected internal (Again) in c#
http://stackoverflow.com/questions/22940317/protected-vs-protected-internal-again-in-c-sharp protect ...