// str 为NSString文本
// font 为文本的字体大小
// maxSize 为你给文本的最大尺寸
+ (CGSize)sizeWithString:(NSString *)str font:(UIFont *)font maxSize:(CGSize)maxSize
{
NSStringDrawingOptions opts = NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading;
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineBreakMode:NSLineBreakByCharWrapping];
NSDictionary *attributes = @{ NSFontAttributeName : font, NSParagraphStyleAttributeName : style };
CGRect contentRect=[str boundingRectWithSize:maxSize options:opts attributes:attributes context:nil];
CGSize resultSize = contentRect.size;
return resultSize;
}
CGSize textSize = [seld sizeWithString:@"文本" font:[UIFont systemFontOfSize:18] maxSize:CGSizeMake(220,MAXFLOAT)];