// 将html内容转化为attributedString
NSString * htmlString = @"<font color='#4085de'> worldcpp </font> <font color='#333333'>来啦 ~</font> 来啦🎉";
NSDictionary *options = @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType };
NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc] initWithData:[htmlString
dataUsingEncoding:NSUnicodeStringEncoding]
options:options
documentAttributes:nil error:nil];
// 富文本字体、颜色等设置
NSDictionary *attributes = @{ NSFontAttributeName: [UIFont systemFontOfSize:16] }
[attributeString addAttributes: attributes, range: NSMageRange(location: 0, length: attributeString.length)];
self.userComeLabel.attributedText = attributeString;
// 计算富文本的size
CGSize textSize = [attributeString boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 26)
options:NSStringDrawingUsesFontLeading
context:nil].size;
CGFloat userComeLabelWidth = textSize.width;
网友评论