iOS中OC加载HTML字符串

转自: https://www.jianshu.com/p/9605e7291fe3

最近项目里面遇见了 HTML 字符串,整理如下:

后台返回字符串的样式

在 iOS 中通常加载 HTML 字符串有两种方式

  • 通过 UILabel 加载富文本的方法加载 HTML 字符串
  • 通过 WebView 加载 HTML 字符串
- (void)viewDidLoad {
    [super viewDidLoad];

1.UILabel 加载 HTML 字符串
NSString * str1 = @"<div>Google(中文名:谷歌),是一家美国的跨国科技企业。</div><div>Google由当时在斯坦福大学攻读理工博士的拉里·佩奇和谢尔盖·布卢姆共同创建,因此两人也被称为“Google Guys”。</div><div>1998年9月4日,Google以私营公司的形式创立,设计并管理一个互联网搜索引擎“Google搜索”。</div&gt";
NSString * str2 = @"<p><br></p&gt";
NSString * str3 = @"<p>qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq</p&gt";
//1.将字符串转化为标准HTML字符串
    str1 = [self htmlEntityDecode:str1];
//2.将HTML字符串转换为attributeString
    NSAttributedString * attributeStr = [self attributedStringWithHTMLString:str1];
    
//3.使用label加载html字符串
    self.label.attributedText = attributeStr;

2.UIWebView 加载HTML字符串
    UIWebView * webView = [[UIWebView alloc]initWithFrame:CGRectMake(20, 300, self.view.frame.size.width - 40, 400)];
    [webView loadHTMLString:str1 baseURL:nil];
    [self.view addSubview:webView];
    self.webView = webView;
}

//将 &lt 等类似的字符转化为HTML中的“<”等 
- (NSString *)htmlEntityDecode:(NSString *)string
{
    string = [string stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    string = [string stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];
    string = [string stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    string = [string stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    string = [string stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"]; // Do this last so that, e.g. @"&amp;lt;" goes to @"&lt;" not @"<"
    
    return string;
}

//将HTML字符串转化为NSAttributedString富文本字符串
- (NSAttributedString *)attributedStringWithHTMLString:(NSString *)htmlString
{
    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
                               NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) };
    
    NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
    
    return [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
}

//去掉 HTML 字符串中的标签
- (NSString *)filterHTML:(NSString *)html
{
    NSScanner * scanner = [NSScanner scannerWithString:html];
    NSString * text = nil;
    while([scanner isAtEnd]==NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:@"<" intoString:nil];
        //找到标签的结束位置
        [scanner scanUpToString:@">" intoString:&text];
        //替换字符
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
    }
    //    NSString * regEx = @"<([^>]*)>";
    //    html = [html stringByReplacingOccurrencesOfString:regEx withString:@""];
    return html;
}

** 注意 **

  • 此处的字符串不是标准的标签的HTML字符串,所以我们首先要调用- (NSString *)htmlEntityDecode:(NSString *)string 方法将字符串转换成标准的HTML字符串,这样才可以进行HTML字符串的加载
  • 实例的第二个字符串中的内容为空,当用 Label 加载的时候只是两行空白数据,此时可以调用- (NSString *)filterHTML:(NSString *)html 方法,去掉标签,将HTML字符串转换为常用的字符串样式



作者:断剑
链接:https://www.jianshu.com/p/9605e7291fe3
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

//本人使用UIWebView 加载公告详情,后台返回图文样式带有 字符:

&lt;p style=&quot;white-space: normal;&quot;&gt;的撒大萨达所&lt;/p&gt;&lt;p style=&quot;white-space: normal;&quot;&gt;发送到发送到啥的&lt;/p&gt;&lt;p style=&quot;white-space: normal;&quot;&gt;的撒大萨达所&lt;/p&gt;&lt;p style=&quot;white-space: normal;&quot;&gt;发送到发送到啥的&lt;/p&gt;&lt;p style=&quot;white-space: normal;&quot;&gt;的撒大萨达所&lt;/p&gt;&lt;p style=&quot;white-space: normal;&quot;&gt;发送到发送到啥的&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;http://192.168.15.106/public/upload/temp/2019/01-29/8a30cf5d53222638896ff4030f9b23d8.jpg&quot; title=&quot;8a30cf5d53222638896ff4030f9b23d8.jpg&quot; alt=&quot;8a30cf5d53222638896ff4030f9b23d8.jpg&quot;/&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;的撒大萨达所&lt;/p&gt;&lt;p&gt;发送到发送到啥的&lt;/p&gt;

页面显示不正常,通过使用本文的方法

//将 &lt 等类似的字符转化为HTML中的“<”等 
- (NSString *)htmlEntityDecode:(NSString *)string
{
    string = [string stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    string = [string stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];
    string = [string stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    string = [string stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    string = [string stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"]; // Do this last so that, e.g. @"&amp;lt;" goes to @"&lt;" not @"<"
    
    return string;
}

解决了显示问题加载到了页面上来

猜你喜欢

转载自blog.csdn.net/zhanglizhi111/article/details/86714008