首页 > 编程语言 > 详细

html语言解析为属性字符串NSMutableAttributedString

时间:2016-01-19 17:18:20      阅读:137      评论:0      收藏:0      [点我收藏+]

 

NSString *htmlString=[NSString stringWithFormat:@"<html><body>%@</body></html>",html语言字段];

NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{     NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0, attrStr.length)];

 

如下所示(即通过一个网络请求设置label的属性字符串):

[HKNetWorkEngine queryInfoNewsWithDict:dict success:^(NSDictionary *resultDict) {

        if ([[resultDict objectForKey:@"code"] isEqualToString:@"1000"]) {

                NSDictionary *newsInfo = [resultDict objectForKey:@"newsinfo"];

                NSString *htmlString=[NSString stringWithFormat:@"<html><body>%@</body></html>",[newsInfo objectForKey:@"newsContent"]];

                NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

                [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0, attrStr.length)];

                label.attributedText = attrStr;

            }

        } ];

html语言解析为属性字符串NSMutableAttributedString

原文:http://www.cnblogs.com/xiaobaichangan/p/5142666.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!