//根据字符串计算高度方法:
- (CGFloat)getHeightLineWithString:(NSString *)string withWidth:(CGFloat)width withFont:(UIFont *)font {
//最大允许绘制的文本范围
CGSize size = CGSizeMake(width, 2000);
//配置计算时的行截取方法,和contentLabel对应
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:10];
//配置计算时的字体的大小
NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:style};
//如果想保留多个枚举值,则枚举值中间加按位或|即可,并不是所有的枚举类型都可以按位或,只有枚举值的赋值中有左移运算符时才可以
CGFloat height = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size.height;
return height;
}
网友评论