一、固定高度获取文本宽度
-(CGFloat)getWidthWithText:(NSString*)text height:(CGFloat)height font:(CGFloat)font{ CGRectrect = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil]; return rect.size.width;}
使用:
CGFloat labelwidth =ceilf([self getWidthWithText:textheight:58font:45]);
二、固定宽度获取文本高度
- (CGFloat)getLabelHeightWithText:(NSString *)text width:(CGFloat)width font: (CGFloat)font
{
CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont ZKYLTFont:font]} context:nil];
return rect.size.height;
}