AppKit 中是否有任何等效的方法(对于 Mac OS X 上的 Cocoa)与 UIKit 的功能相同[NSString sizeWithFont:constrainedToSize:]
?
如果没有,我该如何获得将特定字符串限制为宽度/高度所需的空间量?
更新:下面是我正在使用的一段代码,我希望它会产生我想要的结果。
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize: [NSFont smallSystemFontSize]], NSFontAttributeName,
[NSParagraphStyle defaultParagraphStyle], NSParagraphStyleAttributeName,
nil];
NSSize size = NSMakeSize(200.0, MAXFLOAT);
NSRect bounds;
bounds = [@"This is a really really really really really really really long string that won't fit on one line"
boundingRectWithSize: size
options: NSStringDrawingUsesFontLeading
attributes: attributes];
NSLog(@"height: %02f, width: %02f", bounds.size.height, bounds.size.width);
我希望输出宽度为 200,高度大于单行的高度,但它会产生:
height: 14.000000, width: 466.619141
谢谢!