根据Html文本设置控件的尺寸

//先把html文本转换成富文本内容

NSString * htmlString;

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]

initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding]

options:@{

NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType

}

documentAttributes:nil error:nil];

//再根据富文本内容计算尺寸

NSDictionary * attributesDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];

NSRange  range = NSMakeRange(0, attributedString.length);

[attributedString addAttributes:attributesDic range:range];

NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;

CGRect rect = [attributedString boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)

options:options

context:nil];

你可能感兴趣的:(根据Html文本设置控件的尺寸)