宽
- (CGFloat)calculateRowWidth(NSString *)string {
NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
CGRect rect = [string boundingRectWithSize:CGSizeMake(0, 30)/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading attributes:dic context:nil];
return rect.size.width;
}
高
- (CGFloat)calculateRowHeight:(NSString *)string fontSize:(NSInteger)fontSize
{
NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}
CGRect rect = [string boundingRectWithSize:CGSizeMake(self.view.width - 30, 0)/*计算高度要先指定宽度*/ options:NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading attributes:dic context:nil];
return rect.size.height;
}