美文网首页
ioswkwebview 显示html源码转义符的问题2019-

ioswkwebview 显示html源码转义符的问题2019-

作者: iOS打怪升级 | 来源:发表于2019-10-30 18:40 被阅读0次
NSString *dataString = @"这是一个标题&lt br/>这是第二行";

//先去除转义字符
dataString = [dataString stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
dataString = [dataString stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
dataString = [dataString stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];
//html 的meta最好加上<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no\">这样在移动设备显示起来效果更好



NSString *htmlFormat = @"<!DOCTYPE html> <html lang=\"cn\"> <head> <meta charset=\"utf-8\"> <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no\"> </head> <body> %@ </body> </html>";

NSString *htmlContent = [NSString stringWithFormat:htmlFormat,dataString];

UIWebView *web=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height-64)];

web.scrollView.bounces=NO;//不弹跳

web.userInteractionEnabled=YES;

web.backgroundColor=[UIColor clearColor];

[web loadHTMLString:htmlContent baseURL:nil];
两个分类方法:
  1. 变成转义符的形式
- (NSString *) escapeHTML{
    NSMutableString *s = [NSMutableString string];
    
    NSUInteger start = 0;
    NSUInteger len = [self length];
    NSCharacterSet *chs = [NSCharacterSet characterSetWithCharactersInString:@"<>&\""];
    
    while (start < len) {
        NSRange r = [self rangeOfCharacterFromSet:chs options:0 range:NSMakeRange(start, len-start)];
        if (r.location == NSNotFound) {
            [s appendString:[self substringFromIndex:start]];
            break;
        }
        
        if (start < r.location) {
            [s appendString:[self substringWithRange:NSMakeRange(start, r.location-start)]];
        }
        
        switch ([self characterAtIndex:r.location]) {
            case '<':
                [s appendString:@"&lt;"];
                break;
            case '>':
                [s appendString:@"&gt;"];
                break;
            case '"':
                [s appendString:@"&quot;"];
                break;
                //          case '…':
                //              [s appendString:@"&hellip;"];
                //              break;
            case '&':
                [s appendString:@"&amp;"];
                break;
        }
        
        start = r.location + 1;
    }
    
    return s;
}
2. 去除转义符,变成普通的标签<>

- (NSString *) unescapeHTML{
    NSMutableString *s = [[NSMutableString alloc] init];
    NSMutableString *target = [self mutableCopy];
    NSCharacterSet *chs = [NSCharacterSet characterSetWithCharactersInString:@"&"];
    
    while ([target length] > 0) {
        NSRange r = [target rangeOfCharacterFromSet:chs];
        if (r.location == NSNotFound) {
            [s appendString:target];
            break;
        }
        
        if (r.location > 0) {
            [s appendString:[target substringToIndex:r.location]];
            [target deleteCharactersInRange:NSMakeRange(0, r.location)];
        }
        
        if ([target hasPrefix:@"&lt;"]) {
            [s appendString:@"<"];
            [target deleteCharactersInRange:NSMakeRange(0, 4)];
        } else if ([target hasPrefix:@"&gt;"]) {
            [s appendString:@">"];
            [target deleteCharactersInRange:NSMakeRange(0, 4)];
        } else if ([target hasPrefix:@"&quot;"]) {
            [s appendString:@"\""];
            [target deleteCharactersInRange:NSMakeRange(0, 6)];
        } else if ([target hasPrefix:@"&#39;"]) {
            [s appendString:@"'"];
            [target deleteCharactersInRange:NSMakeRange(0, 5)];
        }else if ([target hasPrefix:@"&#039;"]) {
            [s appendString:@"'"];
            [target deleteCharactersInRange:NSMakeRange(0, 6)];
        } else if ([target hasPrefix:@"&amp;"]) {
            [s appendString:@"&"];
            [target deleteCharactersInRange:NSMakeRange(0, 5)];
        } else if ([target hasPrefix:@"&hellip;"]) {
            [s appendString:@"…"];
            [target deleteCharactersInRange:NSMakeRange(0, 8)];
        } else {
            [s appendString:@"&"];
            [target deleteCharactersInRange:NSMakeRange(0, 1)];
        }
    }
    
    return s;
}
  _content = [_content unescapeHTML];
    _content = [NSString stringWithFormat:@"<html><head><meta name=\"viewport\" content=\"width=%f,initial-scale=1.0,maximum-scale=1.0,user-scalable=0\" /><style>img {max-width: 100%% ;height: auto;} \ntable {max-width: 100%% ;height: auto;}</style></head><body>%@</body></html>", (kScreen_Width - 30), _content];

相关文章

  • ioswkwebview 显示html源码转义符的问题2019-

    两个分类方法: 变成转义符的形式 2. 去除转义符,变成普通的标签<>

  • html标签转义符

    tag The element was used to surround HTML example t...</p></li><li id="r3"><a href="/user/uqlnkxtx.html"><img src="https://img.haomeiwen.com/us15014728/0d092bf7-d2d0-4dce-8ba8-a2585f0bbb25.jpg" width="80"></a><h3><a href="/subject/artxsltx.html">HTML 转义符的用法</a></h3><p>HTML 空格转义符的用法记录一下,空格的转义字符分为如下几种:&160#;不断行的空白(1个字符宽度) &819...</p></li><li id="r4"><h3><a href="/subject/kumctxtx.html">常见错误汇总</a></h3><p>1,分隔符的转义问题</p></li><li id="r5"><a href="/user/ivyjfxtx.html"><img src="https://img.haomeiwen.com/is12980337/3a4f4bfe6ee10a89.png" width="80"></a><h3><a href="/subject/ixhkkqtx.html">Android 常用转义字符</a></h3><p>写过html代码的朋友们应该知道,html支持转义字符,可以通过转义字符实现显示空格、缩进、换行等内容。在Andr...</p></li><li id="r6"><a href="/user/bnxhsttx.html"><img src="https://img.haomeiwen.com/us5115882/94587fdd-eb33-4431-8b85-42c3e869159e" width="80"></a><h3><a href="/subject/zwxrcqtx.html">ios 调用writeToFile将NSData图片缓存沙盒失败</a></h3><p>如题,先确认NSData数据没有问题,如果失败,那么可能是生成的图片名称有问题:比如含有html的转义符'%2f'...</p></li><li id="r7"><a href="/user/pxhbkttx.html"><img src="https://img.haomeiwen.com/us3061645/b0774997-0cab-4d5a-bccb-e8f6a903cc69.png" width="80"></a><h3><a href="/subject/yrxtmxtx.html">JS转换HTML转义符</a></h3><p>http://www.cnblogs.com/leejersey/p/4568092.html //去掉html标...</p></li><li id="r8"><h3><a href="/subject/kewbnctx.html">React中的HTML转义写法</a></h3><p>在JSX中输出固定内容直接使用UTF-8字符 版权 © 使用HTML转义字符 版权 © 或者十进制的转义字符 版权...</p></li><li id="r9"><h3><a href="/subject/jzsctxtx.html">databinding绑定list对象报错</a></h3><p>databinding绑定list对象,由于“<”,“>”符号会报错,需要转义符替换,替换后会显示红色,这是AS的...</p></li><li id="r10"><a href="/user/wkvilxtx.html"><img src="https://img.haomeiwen.com/is15425820/9c74bd330b51ef20.png" width="80"></a><h3><a href="/subject/ysxhvhtx.html">微信小程序-加载html代码并替换html转义符</a></h3><p>最近改小程序bug,需要加载html代码,并且后台返回的html代码里有转义符(类似&quot; <),身为一个安...</p></li></ul></div></div><div id="comments"><h2>网友评论</h2><ul></ul></div><ul></ul><div class="article_by"><div class="bytitle"><p>本文标题:<font color="#666">ioswkwebview 显示html源码转义符的问题2019-</font></p><p>本文链接:<font color="#666">https://www.haomeiwen.com/subject/jolavctx.html</font></p></div></div></div><div class="meiwen-left fl mb"><div class="title ht"><h3>延伸阅读</h3><div class="share fr" id="bdshare"></div><i class="lt"></i><i class="rt"></i></div><div class="related"><ul class="related-list"><li><a class="meiwen" href="/mw/lbyctttx.html">那年盛夏诗歌</a></li><li><a class="meiwen" href="/sy/kbyctttx.html">环境监察队工作总结范文</a></li><li><a class="meiwen" href="/sy/hbyctttx.html">优秀教师学习心得范文</a></li><li><a class="meiwen" href="/sy/cbyctttx.html">华胥引的读后感300字</a></li><li><a class="meiwen" href="/yw/qbyctttx.html">《Its red》教学反思范文</a></li><li><a class="meiwen" href="/sy/fbyctttx.html">农资购销的合同范本</a></li><li><a class="meiwen" href="/sy/xbyctttx.html">竞选中队委优秀演讲稿</a></li><li><a class="meiwen" href="/yw/tbyctttx.html">辞金蹈海的成语解释</a></li><li><a class="meiwen" href="/ja/zvyctttx.html">《世纪宝鼎》公开课教案设计</a></li></ul></div><div class="picture" ><ul class="picture-list"><li><a class="meiwen" href="/article/69641.html"><img class="lazy" data-original="/uploads/allimg/1611/1-1611292156350-L.jpg" width="150" height="120" alt="因为爱你,所以牵挂"/><span>因为爱你,所以牵挂</span></a></li><li><a class="meiwen" href="/article/84632.html"><img class="lazy" data-original="/uploads/userup/2800/14V4C648-O08.jpg" width="150" height="120" alt="今生今世红尘醉——美到心碎的古诗词"/><span>今生今世红尘醉——美到</span></a></li><li><a class="meiwen" href="/article/53482.html"><img class="lazy" data-original="/uploads/allimg/1609/1-160ZPU1030-L.jpg" width="150" height="120" alt="一个90后的内心独白"/><span>一个90后的内心独白</span></a></li><li><a class="meiwen" href="/article/3639.html"><img class="lazy" data-original="/uploads/userup/1159/1432YGR-9294.jpg" width="150" height="120" alt="致已逝去的高中年华"/><span>致已逝去的高中年华</span></a></li></ul></div></div><div class="meiwen-left fl mb"><div id="pinglun"><div id="SOHUCS"></div></div></div><div class="meiwen-left fl"><div class="title ht"><h3>深度阅读</h3><ul class="list"><li><font color="#A2B088">您也可以注册成为美文阅读网的作者,发表您的原创作品、分享您的心情!</font></li></ul><i class="lt"></i><i class="rt"></i></div><ul class="tags"><li><a class="meiwen" href="/huati/qingrenjie/">情人节</a></li><li><a class="meiwen" href="/huati/muqinjie/">母亲节</a></li><li><a class="meiwen" href="/huati/chongyangjie/">重阳节</a></li><li><a class="meiwen" href="/huati/qingmingjie/">清明节</a></li><li><a class="meiwen" href="/huati/duanwujie/">端午节</a></li><li><a class="meiwen" href="/huati/zhishujie/">植树节</a></li><li><a class="meiwen" href="/huati/yuanxiaojie/">元宵节</a></li><li><a class="meiwen" href="/huati/funvjie/">妇女节</a></li><li><a class="meiwen" href="/huati/yurenjie/">愚人节</a></li><li><a class="meiwen" href="/huati/shengdanjie/">圣诞节</a></li><li><a class="meiwen" href="/huati/fuqinjie/">父亲节</a></li><li><a class="meiwen" href="/huati/jiaoshijie/">教师节</a></li><li><a class="meiwen" href="/huati/ertongjie/">儿童节</a></li><li><a class="meiwen" href="/huati/laodongjie/">劳动节</a></li><li><a class="meiwen" href="/huati/qingnianjie/">青年节</a></li><li><a class="meiwen" href="/huati/jianjunjie/">建军节</a></li><li><a class="meiwen" href="/huati/wanshengjie/">万圣节</a></li><li><a class="meiwen" href="/huati/pinganye/">平安夜</a></li><li><a class="meiwen" href="/huati/guanggunjie/">光棍节</a></li><li><a class="meiwen" href="/huati/zhongqiujie/">中秋节</a></li><li><a class="meiwen" href="/huati/guoqingjie/">国庆节</a></li><li><a class="meiwen" href="/huati/ganenjie/">感恩节</a></li><li><a class="meiwen" href="/huati/labajie/">腊八节</a></li><li><a class="meiwen" href="/huati/">更多话题</a></li></ul></div><div class="cl"></div></div><div class="article-right"><div class="meiwen-right fr mb"><dt class="title hd"><h3>栏目导航</h3><i class="lt"></i><i class="rt"></i></dt><ul class="list-nav"><a class="meiwen" href="/c/hqtttttx.html">摄影</a><a class="meiwen" href="/c/vqtttttx.html">故事</a><a class="meiwen" href="/c/uttttttx.html">互联网</a><a class="meiwen" href="/c/cttttttx.html">读书</a><a class="meiwen" href="/c/yttttttx.html">旅行</a></ul></div></div><div class="meiwen-right fr mb"><div class="title hd"><h3>热点阅读</h3><i class="lt"></i><i class="rt"></i></div><ul class="right-list"><li><a class="meiwen" href="/subject/tnlavctx.html">Java技术面试问题</a></li><li><a class="meiwen" href="/subject/zglavctx.html">爱情解</a></li><li><a class="meiwen" href="/subject/aglavctx.html">我二十二岁的穷困潦倒</a></li><li><a class="meiwen" href="/subject/wglavctx.html">那一夜她喝醉了</a></li><li><a class="meiwen" href="/subject/vglavctx.html">单细胞交响乐2-scRNA从实验到下游简介</a></li><li><a class="meiwen" href="/subject/mglavctx.html">爱情在哪里</a></li><li><a class="meiwen" href="/subject/yglavctx.html">随笔</a></li><li><a class="meiwen" href="/subject/eglavctx.html">流浪猫:能帮我活过这个冬天吗?我也想有个家!</a></li><li><a class="meiwen" href="/subject/jglavctx.html">2019-10-30</a></li><li><a class="meiwen" href="/subject/lglavctx.html">寂寞寂寞就好</a></li></ul></div></div></div></div><div class="footer"><p class="p_1"><a href="/about/aboutus.html">关于我们</a>|<a href="/about/service.html">服务条款</a>|<a href="/about/contact.html">联系我们</a>|<a href="/subject/jolavctx.html">ioswkwebview 显示html源码转义符的问题2019-</a>|<a href="/about/tougao.html">投稿指南</a>|<a href="/sitemap.html">网站地图</a>|<a href="/rssmap.html">RSS订阅</a>|<a href="/about/paiban.html">排版工具</a>|<a href="javascript:vod(0);" onclick="Mobileurl()">手机版</a></p><p class="p_2">提供<a href="/meiwen/zhaichao/">经典美文摘抄</a>,<a href="/sanwen/">优美散文欣赏</a>,<a href="/shige/xiandai/">现代诗歌精选</a>,<a href="/xiaoshuo/">短篇小说</a>,<a href="/diary/suibi/">心情随笔</a>,<a href="/qingshu/">表白情书</a><a href="/fanwen/">范文</a>,<a href="/gushi/">故事会在线阅读</a>欣赏</p><p class="p_2">Copyright &copy; 2014-2023 <a href="/">Haomeiwen.com</a> All Rights Reserved. <a href="/">好美文阅读网</a> 版权所有</p><p class="p_2">备案信息:<i></i>桂公网安备 45052102000051号 · 桂ICP备13007215号-3</p><div class="footer_service"><p class="p_2">本站所收录作品、热点评论等信息部分来源互联网,目的只是为了系统归纳学习和传递资讯</p><p class="p_2">所有作品版权归原创作者所有,与本站立场无关,如不慎侵犯了你的权益,请联系我们告知,我们将做删除处理!</p></div></div><script src="https://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script><script type="text/javascript" src="https://apps.bdimg.com/libs/jquery-lazyload/1.9.5/jquery.lazyload.min.js"></script><script type="text/javascript">$("img.lazy").lazyload({effect : "fadeIn"});</script><script type="text/javascript" src="/img/jquery.meiwen.min.js"></script></body></html>