1.先上個自己沒適配的圖,這個圖沒顯示全,因為用的是webview 所以 用的是webView的代理事件 解決
2.上代碼
NSString *injectionJSString =
@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth=375;" // UIWebView中顯示的圖片最大寬度
"for(i=0;i <document.images.length;i++){"
"myimg = document.images[i];"
"oldwidth = myimg.width;"
"if(oldwidth > maxwidth){"//原圖大於最大寬度
"if(myimg.width>myimg.height){"//原圖的寬度大於高度
"myimg.width=maxwidth*2/3;"
"myimg.height = myimg.width*myimg.height/oldwidth;" //修改高度
"}else{"//原圖的寬度小於高度
"myimg.width=maxwidth*2/3;"
"myimg.height = myimg.width*myimg.height/oldwidth;" //修改高度
"}"
"}"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);";
[webView stringByEvaluatingJavaScriptFromString:injectionJSString];
[webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
3.然后出的效果圖是這樣的
4.既然寫到這里了,我就順便把計算webView的內容高度也寫了吧
5.上代碼
NSString *injectionJSString2 = @"var script = document.createElement('meta');"
"script.name = 'viewport';"
"script.content=\"initial-scale=1.0,maximum-scale=1.0, minimum-scale=1.0, user-scalable=no\";"
"document.getElementsByTagName('head')[0].appendChild(script);";
[webView stringByEvaluatingJavaScriptFromString:injectionJSString2];
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'"];//修改百分
CGRect frame = webView.frame;
frame.size.width = SKDeviceWidth;
frame.size.height = 1;
webView.frame = frame;
if (FourInch) {
frame.size.height = webView.scrollView.contentSize.height+25;
}else if (FourOfSevenInch )
{
frame.size.height = webView.scrollView.contentSize.height+15;
}else if (FiveOfFifthInch)
{
frame.size.height = webView.scrollView.contentSize.height+15;
}
6.其實方法有很多,不管是上面的適配還是得到高度,還可以用另外一種方法統一解決
7.這個 我就只上一個關鍵代碼
//獲取webView 中的所有H5內容
NSString *str=[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"];
這個數據獲得,能得到所有H5代碼的標簽,內容,等等 通過這個 可以自己拿到字符去改變,嘿嘿 這個我沒做,不過可以走通