获取系统默认字符串高度获取如下:
- (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;
// 当你是把获得的高度来布局控件的View的高度的时候.size转化为ceilf(size.height)。
return ceilf(size.height);
}
获取含有行间距的字符串高度:
- (CGFloat)getString:(NSString *)string lineSpacing:(CGFloat)lineSpacing font:(UIFont*)font width:(CGFloat)width {
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineSpacing = lineSpacing;
NSDictionary *dic = @{ NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle };
CGSize size = [string boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
return ceilf(size.height);
}