- (CGFloat)getStringHeightWithText:(NSString *)text font:(UIFont *)font viewWidth:(CGFloat)width {
// 设置文字属性要和对应label的一致
NSDictionary *attrs = @{NSFontAttributeName :font};
CGSize maxSize = CGSizeMake(width, MAXFLOAT);
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
// 计算文字占据的宽高
CGSize size = [text boundingRectWithSize:maxSize options:options attributes:attrs context:nil].size;
return ceilf(size.height);
}
网友评论