gpt4 book ai didi

macos - 当磅值非常小时,sizeWithAttributes 在 mac 上使用 NSFont 给出了错误的高度

转载 作者:行者123 更新时间:2023-12-05 05:21:50 29 4
gpt4 key购买 nike

我正在开发一款 Mac 应用程序,可以让用户放大内容。我需要测量我呈现的文本的大小。当我缩放字体以进行缩放时,字体的点大小非常小(即 0.001)。当我使用 [NSString sizeWithAttributes:] 来获取字符串的大小时,我得到了正确的宽度,并且高度始终为 1.0。我还有一个 iOS 应用程序,这个渲染类在 iOS 和 Mac 上都使用。在 iOS 上,当我使用点大小为 .001 的 UIFont 时,我在调用 sizeWithAttributes 时获得了正确的高度。

最佳答案

我也遇到了同样的问题。以下示例代码

#if TARGET_OS_IOS == 0
#define UIFont NSFont
#define NSStringFromCGSize NSStringFromSize
#endif

UIFont *uiFont = [UIFont fontWithName:@"Courier" size:19];
CGSize boxSize = [@"1" sizeWithAttributes:@{NSFontAttributeName:uiFont}];
NSLog(@"boxSize %@", NSStringFromCGSize(boxSize));

iOS 上的产量 boxSize {11.40185546875, 19},在 macOS boxSize {11.40185546875, 24} 上。

我的解决方法:使用 CTFont。

CTFontRef _contentFont = CTFontCreateWithName((__bridge CFStringRef)@"Courier", 19, NULL);
CGGlyph glyphs[1];
UniChar chars[1];
chars[0] = '1';
bool rc = CTFontGetGlyphsForCharacters(_contentFont, chars, glyphs, 1);
if (rc) {
double advance = CTFontGetAdvancesForGlyphs(_contentFont, kCTFontOrientationDefault, glyphs, NULL, 1);
boxSize.width = advance;
boxSize.height = CTFontGetAscent(_contentFont) + CTFontGetDescent(_contentFont);
}

关于macos - 当磅值非常小时,sizeWithAttributes 在 mac 上使用 NSFont 给出了错误的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41986584/

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