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

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

问题描述

AppKit中有任何等效的方法(对于Mac OS X上的Cocoa)与UIKit的 [NSString sizeWithFont:constrainedToSize:]

如果没有,我如何获取渲染特定字符串所需的空间大小限制为width / height?



Update:下面是我使用的代码段,我期望会产生我之后的结果。

  NSDictionary * attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize:[NSFont smallSystemFontSize]],NSFontAttributeName,
[NSParagraphStyle defaultParagraphStyle],NSParagraphStyleAttributeName,
nil];
NSSize size = NSMakeSize(200.0,MAXFLOAT);
NSRect bounds;

bounds = [@这真的真的真的真的真的很长的字符串,不适合在一行
boundingRectWithSize:size
选项:NSStringDrawingUsesFontLeading
attributes:attributes];

NSLog(@height:%02f,width:%02f,bounds.size.height,bounds.size.width);

我希望输出宽度为200,高度将大于但它生成:

  height:14.000000,width:466.619141 


您应该能够在狮子会和以后的日子以正常的方式做事情。下面描述的问题已修复。






没有办法准确测量当前Mac OS X API之间的文本。



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



[显然我已经提交了核心文本:8666756。复制一个未指定的其他错误。对于Cocoa,我提交了9022238关于Dave建议的方法,并将明天提交两个NSLayoutManager bug。]



这是我找到的最接近的解决方案。


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!

解决方案

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.

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

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