webView调整图片宽度适合屏幕
1、使用 loadRequest: 加载url时
-(void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *script = [NSString stringWithFormat:
@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var img;"
"var maxwidth=%f;"
"for(i=0;i <document.images.length;i++){"
"img = document.images[i];"
"if(img.width > maxwidth){"
"img.width = maxwidth;"
"}"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);", self.view.frame.size.width - 20];
[webView stringByEvaluatingJavaScriptFromString: script];
[webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
}
2、 使用 loadHTMLString 加载字符串
NSInteger width = self.webView.frame.size.width * 0.95;
NSString *string = [NSString stringWithFormat:@"<head><style>img{max-width:%ldpx !important;}ul {margin:0; padding:0; text-align:left;}</style><head>", (long)width];
NSString *newHtml = [self HTML:model.body];
NSString *str = [NSString stringWithFormat:@"%@%@", string, newHtml];
[self.webView loadHTMLString:str baseURL:nil];