在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);
Run Code Online (Sandbox Code Playgroud)
我希望输出宽度为200,高度将大于单行的高度,但它会产生:
height: 14.000000, width: 466.619141
Run Code Online (Sandbox Code Playgroud)
谢谢!
下面列出的方法都没有给出 Cocoa 中文本的精确可见边界。我们如何获得它?
NSString的boundingRectWithSize:options:attributes:NSAttributedString的boundingRectWithSize:options:NSLayoutManager的usedRectForTextContainer: