Hybrid----UIWebView的HTML页面

来源:互联网 发布:a1科密考勤系统数据库 编辑:程序博客网 时间:2024/04/19 06:42

最近在做一些hybird框架的项目,对于embed的UIWebView,其宽度一般由Native app 指定,对于HTML页面

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

其中若是width指定为device-width,那么embed的UIWebView的宽不应设置小于device-width的值,如ipad的宽为768



在webView中查看HTML页面源代码

- (void)webViewDidFinishLoad:(UIWebView *)webView_ {    NSString *jsToGetHTMLSource = @"document.getElementsByTagName('html')[0].innerHTML";    NSString *HTMLSource = [webView_ stringByEvaluatingJavaScriptFromString:jsToGetHTMLSource];    NSLog(@"%@",HTMLSource);}

在webView中调整HTML页面宽度
- (void)adjustPageWidth:(UIWebView *)webView_ {    NSString *widthStr = [NSString stringWithFormat:@"%f",webView_.frame.size.width];    NSString * strJS = [NSString stringWithFormat:@"function adjustPageWidth(){var metas = document.getElementsByTagName(\"meta\");var strMeta=new String();var strTemp;for(var i=0;i < metas.length;i++){if(metas[i].name == \"viewport\"){metas[i].setAttribute('content',\"width=%@, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\");console.log(metas[i].name);}}}",widthStr];    [webView_ stringByEvaluatingJavaScriptFromString:strJS];    NSString * strFunctionJS= @"adjustPageWidth();";    [webView_ stringByEvaluatingJavaScriptFromString:strFunctionJS];}
在webViewDidFinishLoad中调用调整页面宽度函数
- (void)webViewDidFinishLoad:(UIWebView *)webView_ {    DebugLog(@"%@",NSStringFromSelector(_cmd));         [self adjustPageWidth:webView_];}