方法1:
// UIWebView
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 15, SCREEN_WIDTH, SCREEN_HEIGHT - NAVBAR_HEIGHT - 15)];
_webView.backgroundColor = viewAndTableViewBackgroundColor;
_webView.delegate = self;
_webView.opaque = NO; // 去除底部黑块
_webView.scrollView.showsHorizontalScrollIndicator = NO;
/**
* _infoModel.content就是后台返回的带有html标签的字符串
* " $img[p].style.width = '100%%';\n"--->就是设置图片的宽度的
* 100%代表正好为屏幕的宽度
*/
NSString *htmlString = [NSString stringWithFormat:@"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-size:15px;}\n"
"</style> \n"
"</head> \n"
"<body>"
"<script type='text/javascript'>"
"window.onload = function(){\n"
"var $img = document.getElementsByTagName('img');\n"
"for(var p in $img){\n"
" $img[p].style.width = '100%%';\n"
"$img[p].style.height ='auto'\n"
"}\n"
"}"
"</script>%@"
"</body>"
"</html>", _infoModel.content];
[_webView loadHTMLString:htmlString baseURL:nil];
[self.view addSubview:_webView];
方法2:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (SVPWebViewTypeActivity_Narrow == self.webViewType){
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];
[webView stringByEvaluatingJavaScriptFromString:meta];
}
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
}