方法一:只有字号限制
NSString *string = @"计算文字长度";
CGSize fontSize = [string sizeWithAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName]];
height:16.707031,width:85.680000
方法二:固定宽度计算字符串高度,常用于计算cell的高度
NSString *string = @"计算文字长度计算文字长度计算文字长度计算文字长度计算文字长度计算文字长度计算文字长度";
CGRect fontRect = [string boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
NSLog(@"Width:%f,Height:%f",fontRect.size.width,fontRect.size.height);
Width:99.960000,Height:100.242188