AppKit 中 UIKit 的 [NSString sizeWithFont:constrainedToSize:]

发布于 2024-10-18 02:16:28 字数 1035 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

铁憨憨 2024-10-25 02:16:28

试试这个:

bounds = [value boundingRectWithSize:size options:NSLineBreakByWordWrapping | NSStringDrawingUsesLineFragmentOrigin attributes:attributes];

Try this one:

bounds = [value boundingRectWithSize:size options:NSLineBreakByWordWrapping | NSStringDrawingUsesLineFragmentOrigin attributes:attributes];
·深蓝 2024-10-25 02:16:28

较新的 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.

新人笑 2024-10-25 02:16:28

编辑:您应该能够在 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.

落日海湾 2024-10-25 02:16:28

如果要将字符串限制为特定大小,请使用 -[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 returned NSRect is the size you're looking for.

风筝在阴天搁浅。 2024-10-25 02:16:28

下面是一个更完整的示例,说明如何使用boundingRectWithSize 来执行此操作。

// get the text field
NSTextField* field = (NSTextField*)view;

// create the new font for the text field
NSFont* newFont = [NSFont fontWithName:@"Trebuchet MS" size:11.0];

// set the font on the text field
[field setFont:newFont];

// calculate the size the textfield needs to be in order to display the text properly
NSString* text      = field.stringValue;
NSInteger maxWidth  = field.frame.size.width;
NSInteger maxHeight = 20000;
CGSize constraint   = CGSizeMake(maxWidth, maxHeight);
NSDictionary* attrs = [NSDictionary dictionaryWithObjectsAndKeys:NSFontAttributeName,newFont, nil];
NSRect newBounds    = [text boundingRectWithSize:constraint
                                         options:NSLineBreakByWordWrapping | NSStringDrawingUsesLineFragmentOrigin
                                      attributes:attrs];

// set the size of the text field to the calculated size
field.frame = NSMakeRect(field.frame.origin.x, field.frame.origin.y, field.frame.size.width, newBounds.size.height);

当然,有关其他信息,请查看 Apple 文档:

Here is a more complete example of how you can do this using boundingRectWithSize.

// get the text field
NSTextField* field = (NSTextField*)view;

// create the new font for the text field
NSFont* newFont = [NSFont fontWithName:@"Trebuchet MS" size:11.0];

// set the font on the text field
[field setFont:newFont];

// calculate the size the textfield needs to be in order to display the text properly
NSString* text      = field.stringValue;
NSInteger maxWidth  = field.frame.size.width;
NSInteger maxHeight = 20000;
CGSize constraint   = CGSizeMake(maxWidth, maxHeight);
NSDictionary* attrs = [NSDictionary dictionaryWithObjectsAndKeys:NSFontAttributeName,newFont, nil];
NSRect newBounds    = [text boundingRectWithSize:constraint
                                         options:NSLineBreakByWordWrapping | NSStringDrawingUsesLineFragmentOrigin
                                      attributes:attrs];

// set the size of the text field to the calculated size
field.frame = NSMakeRect(field.frame.origin.x, field.frame.origin.y, field.frame.size.width, newBounds.size.height);

Of course, for additional info, take a look at the Apple documentation:

白鸥掠海 2024-10-25 02:16:28

如果您搜索 NSString 的文档,您将找到“NSString Application Kit Additions Reference”,它与 UIKit 的对应部分非常相似。

-[NSString sizeWithAttributes:]

就是您正在寻找的方法。

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.

-[NSString sizeWithAttributes:]

is the method you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文