码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-01-19 17:18:29      阅读:193      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!