iOS 中通常加载 HTML 字符串

  有时候后台会返回多行文字,其中文字中某写大小,颜色需要特殊订制就会比较麻烦,会用HTML 字符串返回给前台。

假如后台返回的是responseData[@"activityMsg"]   这个,长这样

activityMsg = "<head>

 

    <meta charset="UTF-8">

 

</head>

 

<div style="padding: 0px 0px;">

 

  <p align="left" style="color: #990035;font-size: 13px; padding:0;margin:0">1.本次交易仅支持 信用卡,光大银行手机信用卡</p>

 

  <p align="left" style="color: #990035;font-size: 13px; padding:0;margin:0">2.光大联名信用卡最高满5000元减300元</p>

 

  <p align="left" style="color: #990035;font-size: 13px; padding:0;margin:0">3.使用银行卡满减权益的订单不支持退款</p>

 

</div>"

1,通过 UILabel 加载富文本的方法加载 HTML 字符串

  NSString* str1 = [self htmlEntityDecode:responseData[@"content"]];

            //2.将HTML字符串转换为attributeString

            NSAttributedString * attributeStr = [self attributedStringWithHTMLString:str1];

            

            //3.使用label加载html字符串

            _contentLab.attributedText = attributeStr;

            _contentLab.text  //这个取到的值可以当成字符串往后面的页面去传

            CGSize titleSize = [_contentLab.attributedText boundingRectWithSize:CGSizeMake(kScreenWidth-60, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;

            

            _contentLab.frame=CGRectMake(30, 115, kScreenWidth-60, ceilf(titleSize.height));

//将 &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;

}

  • 通过 WebView 加载 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;

换成wkwebview也行 也是具有这个方法

loadHTMLString:str1 baseURL:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值