AppKit 中 UIKit 的 [NSString sizeWithFont:constrainedToSize:]
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
谢谢!
Is there any equivalent method in AppKit (for Cocoa on Mac OS X) that does the same thing as UIKit's [NSString sizeWithFont:constrainedToSize:]
?
If not, how could I go about getting the amount of space needed to render a particular string constrained to a width/height?
Update: Below is a snippet of code I'm using that I expect would produce the results I'm after.
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);
I would expect that the output width would be 200 and the height would be something greater than the height of a single line, however it produces:
height: 14.000000, width: 466.619141
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
试试这个:
Try this one:
较新的 NSExtendedStringDrawing 类别 API(带有 NSStringDrawingOptions 参数的方法)在单行模式下运行。如果要在多线模式下测量/渲染,需要指定 NSStringDrawingUsesLineFragmentOrigin。
The newer NSExtendedStringDrawing category API (methods with the NSStringDrawingOptions argument) behaves in the single line mode. If you want to measure/render in multi line mode, want to specify NSStringDrawingUsesLineFragmentOrigin.
编辑:您应该能够在 Lion 及更高版本中以正常方式执行操作。下面描述的问题已得到解决。
目前的 Mac OS X API 无法准确测量文本。
有几个 API 承诺可以工作,但实际上并没有。这是其中之一; 核心文本用于此目的的功能是另一个。有些方法返回的结果很接近但错误;有些返回结果似乎毫无意义。我还没有提交任何关于这些的错误,但是当我这样做时,我会将雷达数字编辑到这个答案中。您还应该提交一个错误,并将您的代码包含在一个小型示例项目中。
[显然我已经提交了核心文本:8666756。它已作为未指定的其他错误的重复项而关闭。对于 Cocoa,我提交了关于 Dave 建议的方法的 9022238,明天将提交两个 NSLayoutManager 错误。]
这是我找到的最接近的解决方案。
EDIT: You should be able to do things the normal way in Lion and later. The problems described below have been fixed.
There is no way to accurately measure text among the current Mac OS X APIs.
There are several APIs that promise to work but don't. That's one of them; Core Text's function for this purpose is another. Some methods return results that are close but wrong; some return results that seem to mean nothing at all. I haven't filed any bugs on these yet, but when I do, I'll edit the Radar numbers into this answer. You should file a bug as well, and include your code in a small sample project.
[Apparently I have already filed the Core Text one: 8666756. It's closed as a duplicate of an unspecified other bug. For Cocoa, I filed 9022238 about the method Dave suggested, and will file two NSLayoutManager bugs tomorrow.]
This is the closest solution I've found.
如果要将字符串限制为特定大小,请使用
-[NSStringboundingRectWithSize:options:attributes:]
。返回的NSRect
的.size
是您要查找的大小。If you want to constrain the string to a certain size, you use
-[NSString boundingRectWithSize:options:attributes:]
. The.size
of the returnedNSRect
is the size you're looking for.下面是一个更完整的示例,说明如何使用boundingRectWithSize 来执行此操作。
当然,有关其他信息,请查看 Apple 文档:
Here is a more complete example of how you can do this using boundingRectWithSize.
Of course, for additional info, take a look at the Apple documentation:
如果您搜索
NSString
的文档,您将找到“NSString Application Kit Additions Reference”,它与 UIKit 的对应部分非常相似。就是您正在寻找的方法。
If you search the documentation for
NSString
, you will find the "NSString Application Kit Additions Reference", which is pretty much analogous to the UIKit counterpart.is the method you are looking for.