NSMutableAttributedString 结合 UILabel显示 Html

NSString *htmlString = @" Some html string \n This is some text! ";

NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithData:[self.model.content dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
// 设置 文本的 字体大小
[attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17.0f weight:10.0f] range:NSMakeRange(0, attrStr.length)];
// 动态计算 文本高度
CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(kSCREEN_WIDTH, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil];

UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:17.0f weight:10.0f];
label.numberOfLines = 0;
label.attributedText = attrStr;
label.frame = CGRectMake(0, 0, kSCREEN_WIDTH, rect.size.height);

你可能感兴趣的:(NSMutableAttributedString 结合 UILabel显示 Html)