/></align>”,此种情况导致 UIWebView 识别不了 这些 HTML 便签,会被原封不动的输出来。
解决方案:将特殊字符替换了,图片自适应
- (NSString *)htmlEntityDecode:(NSString *)string
{
//将特殊字符替换了
string = [string stringByReplacingOccurrencesOfString:@""" withString:@"\""];
string = [string stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
string = [string stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
string = [string stringByReplacingOccurrencesOfString:@">" withString:@">"];
string = [string stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
// 图片自适应
string = [NSString stringWithFormat:@"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"</style> \n"
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">"
"</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>",string];
return string;
}
NSString *str1 = [self htmlEntityDecode:self.dataStr];
[webView loadHTMLString:str1 baseURL:[NSURL URLWithString:kdanye]];
就可以啦
参考链接:
http://blog.csdn.net/shane_ying/article/details/51725351
http://blog.csdn.net/wenmingzheng/article/details/50493658?utm_source=itdadao&utm_medium=referral