AppKit 中 UIKit 的 [NSString sizeWithFont:constrainedToSize:] [英] UIKit's [NSString sizeWithFont:constrainedToSize:] in AppKit

查看:21
本文介绍了AppKit 中 UIKit 的 [NSString sizeWithFont:constrainedToSize:]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AppKit 中是否有任何等效的方法(用于 Mac OS X 上的 Cocoa)与 UIKit 的 [NSString sizeWithFont:constrainedToSize:] 做同样的事情?

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);

我希望输出宽度为 200,高度将大于单行的高度,但是它会产生:

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

谢谢!

推荐答案

您应该能够在 Lion 及更高版本中以正常方式进行操作.下面描述的问题已得到修复.

You should be able to do things the normal way in Lion and later. The problems described below have been fixed.

在当前的 Mac OS X API 中,没有办法准确测量文本.

There is no way to accurately measure text among the current Mac OS X APIs.

有几个 API 承诺可以工作,但没有.那就是其中之一;核心为此目的的文本功能是另一个.有些方法返回的结果接近但错误;一些似乎毫无意义的返回结果.我还没有提交任何关于这些的错误,但是当我这样做时,我会将雷达数字编辑到这个答案中.您也应该提交一个错误,并将您的代码包含在一个小型示例项目中.

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.

[显然我已经提交了核心文本一:8666756.它已作为未指明的其他错误的副本而关闭.对于 Cocoa,我就 Dave 建议的方法提交了 9022238,明天将提交两个 NSLayoutManager 错误.]

[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.]

这是我最接近的解决方案已经找到了.

这篇关于AppKit 中 UIKit 的 [NSString sizeWithFont:constrainedToSize:]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆