IOS UIWebView截獲html並修改便簽內容,寬度自適應,iosuiwebview

來源:互聯網
上載者:User

IOS UIWebView截獲html並修改便簽內容,寬度自適應,iosuiwebview

需求:混合應用UIWebView開啟html後,UIWebView有左右捲軸,要去掉左右滾動效果;  方法:通過js截獲UIWebView中的html,然後修改html標籤內容;  執行個體代碼:  伺服器端html

Java代碼  
  1. <html><head>  
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  3. <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">   
  4. <title>網曝四川省一考場時鐘慢半小時 老師稱這就是命</title></head<body>網曝四川省一考場時鐘慢半小時 老師稱這就是命</body></html>  


這樣顯示的結果網頁的最小寬度會是device-width;但有時候不需要這個寬度,就需要修改width=device-width為width=myWidth; 
用戶端代碼

Java代碼  
  1. - (void)webViewDidFinishLoad:(UIWebView *)webView  
  2. {     
  3.     //修改伺服器頁面的meta的值  
  4.     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];  
  5.     [webView stringByEvaluatingJavaScriptFromString:meta];  
  6. }  


這樣問題就可以解決了 

新增代碼: 

Java代碼  
  1. //給網頁增加utf-8編碼  
  2.  [webView stringByEvaluatingJavaScriptFromString:  
  3.  @"var tagHead =document.documentElement.firstChild;"  
  4.   "var tagMeta = document.createElement(\"meta\");"   
  5.   "tagMeta.setAttribute(\"http-equiv\", \"Content-Type\");"   
  6.   "tagMeta.setAttribute(\"content\", \"text/html; charset=utf-8\");"   
  7.   "var tagHeadAdd = tagHead.appendChild(tagMeta);"];  

 

Java代碼  
  1. //給網頁增加css樣式  
  2.     [webView stringByEvaluatingJavaScriptFromString:  
  3.      @"var tagHead =document.documentElement.firstChild;"  
  4.      "var tagStyle = document.createElement(\"style\");"   
  5.      "tagStyle.setAttribute(\"type\", \"text/css\");"   
  6.      "tagStyle.appendChild(document.createTextNode(\"BODY{padding: 20pt 15pt}\"));"  
  7.      "var tagHeadAdd = tagHead.appendChild(tagStyle);"];  



Java代碼  
  1. //攔截網頁圖片  並修改圖片大小        
  2. [webView stringByEvaluatingJavaScriptFromString:  
  3.  @"var script = document.createElement('script');"   
  4.  "script.type = 'text/javascript';"   
  5.  "script.text = \"function ResizeImages() { "   
  6.      "var myimg,oldwidth;"  
  7.      "var maxwidth=380;" //縮放係數   
  8.      "for(i=0;i <document.images.length;i++){"   
  9.          "myimg = document.images[i];"  
  10.          "if(myimg.width > maxwidth){"   
  11.              "oldwidth = myimg.width;"   
  12.              "myimg.width = maxwidth;"   
  13.              "myimg.height = myimg.height * (maxwidth/oldwidth);"   
  14.          "}"   
  15.      "}"   
  16.  "}\";"   
  17.  "document.getElementsByTagName('head')[0].appendChild(script);"];   
  18.   
  19. [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


我用andrwebview載入html文檔自適應

採用百分比來布局,貌似webkit手機瀏覽器 不支援表格代碼吧
 
ios uiwebview可以載入json格式的資料?

可以的,但是你要將json轉換成你的資料結構,xml也一樣,不可能直接去load一個json或者xml資料
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.