IOS 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
IOS UIWebView截获html并修改便签内容,宽度自适应的更多相关文章
- ios UIWebView截获html并修改便签内容(转载)
ios UIWebView截获html并修改便签内容 博客分类: iphone开发iphone开发phoneGap uiwebviewstringByEvaluatingJavaScriptFromS ...
- ios UIWebView截获html并修改便签内容
需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果: 方法:通过js截获UIWebView中的html,然后修改html标签内容: 实例代码: 服务器端 ...
- PHP正则匹配到2个字符串之间的内容,匹配HTML便签内容
PHP正则匹配到2个字符串之间的内容 $preg= '/xue[\s\S]*?om/i'; preg_match_all($preg,"学并思网址xuebingsi.com",$r ...
- UIWebview 截获html并修改内容。
需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果: 方法:通过js截获UIWebView中的html,然后修改html标签内容: 实例代码: 服务 ...
- UIWebView获得内容的高 高度自适应 宽度自适应
UIWebView获得内容的高-作出自适应高的UIWebView- (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *height ...
- android widget 开发实例 : 桌面便签程序的实现具体解释和源代码 (上)
如有错漏请不吝拍砖指正,转载请注明出处,很感谢 桌面便签软件是android上经常使用软件的一种,比方比較早的Sticky Note,就曾很流行, Sticky Note的介绍能够參见 http:// ...
- 魅族便签,是否能成为国内便签应用的No.1?
继前不久锤子科技推出便签 Android 新版后,近期魅族在PRO 6公布会上也公布了最新的魅族便签应用.这一次魅族把便签应用拓展到了整个Android体系,也就是说.其它不论什么的Android手机 ...
- Android一个小巧的记录app(便签或者日记 随心)
入驻博客园两个月今天第一次发随笔,,话不多说,直接上图展示效果 主界面用的RecyclerView的瀑布流(StaggeredGridLayoutManager),同时加上Floatbutton悬浮按 ...
- CSS3+JS 实现的便签应用
概述 利用HTML5新增的 locationStorage 实现的便签应用,没有使用 JQuery,主要是为了练习原生JS的使用,采用响应式开发,在手机端和桌面端都有良好的体验,而且使用CSS3添加了 ...
随机推荐
- Rsync文件同步
Rsync文件同步 本章结构 关于rsync 1.一款增量备份工具,remote sync,远程同步,支持本地复制或者与其他SSH.rsync主机同步,官方网站:http://rsync.samba. ...
- 关于inodes占用100%的问题及解决方法
#df shows no file systems processedPosted by John Quaglieri on 27 July 2012 07:26 AMA df -m command ...
- 关于async与await的FAQ 转
(译)关于async与await的FAQ 传送门:异步编程系列目录…… 环境:VS2012(尽管System.Threading.Tasks在.net4.0就引入,在.net4.5中为其增加了更丰富的 ...
- Part 52 Attributes in C#
- js对象,原型,call,apply浅析
//对象直接量,创建对象最简单的方式是在js里使用对象直接量 var book = { "main title": "js", //属性里有空格,要用引号 &q ...
- php数组编码转换函数的示例
场景说明/问题描述: Ajax提交页面编码为gb2312,数据库编码为utf8,在不更改页面及数据库编码的情况下插入数据. 自定义函数: 代码如下 复制代码 function array_iconv ...
- MySQL写入插入数据优化配置
*innodb_buffer_pool_size 如果用Innodb,那么这是一个重要变量.相对于MyISAM来说,Innodb对于buffer size更敏感.MySIAM可能对于大数据量使用默认的 ...
- 集合框架学习之排序Comparable&Comoarator
1.内置引用数据类型比较(常用) 1.1 Comparable 1.整数.小数Integer Float Double 直接比较基本数据类型的大小 2.字符:比较的Unicode码只差 3.字符串: ...
- 字符集转换: Ansi - Unicode
字符集转换: Ansi - Unicode wstring AnsiToUnicode (const string& strSrc ) { /*!< 分配目标空间 */ ,strSrc. ...
- 嵌入式 linux 移植修改后的libjpeg 实现内存中解码
1.修改libjpeg源码,使之实现内存解码. 修改libjpeg中读取或者输出jpeg文件的函数接口文件jdatadst.c和jdatasrc.c见下面这篇帖子. http://blog.163.c ...