gpt4 book ai didi

cocoa - UIKit 的 [NSString sizeWithFont :constrainedToSize:] in AppKit

转载 作者:行者123 更新时间:2023-12-03 16:08:12 26 4
gpt4 key购买 nike

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

谢谢!

最佳答案

试试这个:

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

关于cocoa - UIKit 的 [NSString sizeWithFont :constrainedToSize:] in AppKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5033598/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com