关于WebView 修改HTML图片

2016-10-11  本文已影响0人  BK_Samuel

1.使用!important

NSString *strTemplateHTML = [NSString stringWithFormat:@"<html><head><style>img{max-width:320.0;height:auto !important;width:auto !important;};</style></head><body style='margin:0; padding:0;'>%@</body></html>", @"insert your html content here"];
[webView loadHTMLString:strTemplateHTML baseURL:nil];

2.使用 js

htmlText=[jsonObj objectForKey:@"content"];
jsString = [NSString stringWithFormat:@"<html> \n"
                                          "<head> \n" 
                                          "<style type=\"text/css\"> \n"         
                                          "body {font-size:%fpx; line-height:%fpx;background-color: transparent;}\n" 
                                          // "img{max-width:305;height:auto !important;width:auto !important;};" 
                                          ".img {width:你设定的值;}"//关键是这句给所有网络图片设宽度                 
                                          "</style> \n" 
                                          "</head> \n" 
                                          "<body>%@</body> \n"
                                          "</html>", fontSize ,line_height,htmlText]; 
NSURL *urlBai=[NSURL URLWithString:ImageWeb_Head]; 
[showWebView loadHTMLString:jsString baseURL: urlBai]; 
showWebView.delegate=self;

代理内调用

-(void)webViewDidFinishLoad:(UIWebView *)webView{
         [webView stringByEvaluatingJavaScriptFromString: @"var script =  document.createElement('script');" "script.type = 'text/javascript';" "script.text = \"function ResizeImages() { " "var imgs = document.getElementsByTagName('img');" "for (var i = 0; i < imgs.length; i ++) {" " var img = imgs[i];" " img.style.width = 你设定的值;"" img.style.height = null;" "}" "}\";" "document.getElementsByTagName('head')[0].appendChild(script);"];
         [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
}
上一篇下一篇

猜你喜欢

热点阅读