http://blog.csdn.net/xunyn/article/details/9013395


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

[html]  view plain copy print ?
  1. <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页面源代码

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

在webView中调整HTML页面宽度
  1. - (void)adjustPageWidth:(UIWebView *)webView_ {  
  2.   
  3.     NSString *widthStr = [NSString stringWithFormat:@"%f",webView_.frame.size.width];  
  4.     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];  
  5.     [webView_ stringByEvaluatingJavaScriptFromString:strJS];  
  6.     NSString * strFunctionJS= @"adjustPageWidth();";  
  7.     [webView_ stringByEvaluatingJavaScriptFromString:strFunctionJS];  
  8.   
  9. }  
在webViewDidFinishLoad中调用调整页面宽度函数
  1. - (void)webViewDidFinishLoad:(UIWebView *)webView_ {  
  2.     DebugLog(@"%@",NSStringFromSelector(_cmd));  
  3.    
  4.         [self adjustPageWidth:webView_];  
  5. }  

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐