gpt4 book ai didi

objective-c - Cocoa 从字体中获取以像素为单位的字符串宽度

转载 作者:太空狗 更新时间:2023-10-30 03:57:22 25 4
gpt4 key购买 nike

我试图从字体和字体大小中找到字符串的宽度(以像素为单位)。我目前正在使用此代码,但它并非 100% 的时间都在工作。还有其他方法吗?

NSSize textSize = [aTextLayer.string sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:@"Bank Gothic Medium", NSFontNameAttribute, [NSNumber numberWithFloat:aTextLayer.fontSize], NSFontSizeAttribute, nil]];

最佳答案

NSAttributedString 被 Application Kit Additions 授予了一个 -size 方法。

NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:
@"Bank Gothic Medium", NSFontNameAttribute,
[NSNumber numberWithFloat:aTextLayer.fontSize], NSFontSizeAttribute,
nil];
NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:aTextLayer.string attributes:attributes];
NSSize size = attributedString.size;

关于objective-c - Cocoa 从字体中获取以像素为单位的字符串宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3692143/

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