直接上代碼
/*
text:文字
font:文字的字體
maxsize:文字的最大寬高
*/
- (CGSize)sizeWithText:(NSString *)text andFont:(NSFont *)font andMaxSize:(CGSize)maxsize{
NSDictionary * dict = @{NSFontAttributeName : font};
return [text boundingRectWithSize:maxsize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
}