1.计算文字长度
NSString* str = @"你好"; const float kFontSize = .f; NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading; CGRect rect = [str boundingRectWithSize:CGSizeMake(MAXFLOAT,MAXFLOAT) options:options attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kFontSize]} context:nil]; CGFloat realHeight = ceilf(rect.size.height);
2.如果限制文字的宽度或高度,则将CGSizeMake(MAXFLOAT,MAXFLOAT)中的相应参数修改成具体的值