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;
}
} ];