根据内容计算宽度
- (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;
}
---------------------
作者:cocoawork丶
来源:CSDN
原文:https://blog.csdn.net/u010170540/article/details/51916511
版权声明:本文为博主原创文章,转载请附上博文链接!
网友评论