gpt4 book ai didi

objective-c - 如何使用 CTFontRef 获取 FontFormat

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

有旧的碳代码使用 FMGetFontFormat 来确定字体是否为“True Type”。由于该 API 已弃用,有没有办法使用 CTFontRef 找到相同的 API。我使用了 CTFontCopyAttribute,但它返回 CTTypeRef 并且我无法获取该值?有什么建议吗?

最佳答案

CTTypeRef 是通用类型。如果您阅读 kCTFontFormatAttribute 常量的文档,它们会指出:

The value associated with this key is an integer represented as a CFNumberRef object containing one of the constants in “Font Format Constants.”

这意味着您需要将该属性视为数字,然后可以将其转换为 short 并根据 CTFontFormat 的已知值进行检查:

//get an array of all the available font names
CFArrayRef fontFamilies = CTFontManagerCopyAvailableFontFamilyNames();

//loop through the array
for(CFIndex i = 0; i < CFArrayGetCount(fontFamilies); i++)
{
//get the current name
CFStringRef fontName = CFArrayGetValueAtIndex(fontFamilies, i);

//create a CTFont with the current font name
CTFontRef font = CTFontCreateWithName(fontName, 12.0, NULL);

//ask it for its font format attribute
CFNumberRef fontFormat = CTFontCopyAttribute(font, kCTFontFormatAttribute);

//release the font because we're done with it
CFRelease(font);

//if there is no format attribute just skip this one
if(fontFormat == NULL)
{
NSLog(@"Could not determine the font format for font named %@.", fontName);
continue;
}

//get the font format as a short
SInt16 format;
CFNumberGetValue(fontFormat, kCFNumberSInt16Type, &format);

//release the number because we're done with it
CFRelease(fontFormat);

//create a human-readable string based on the format of the font
NSString* formatName = nil;
switch (format) {
case kCTFontFormatOpenTypePostScript:
formatName = @"OpenType PostScript";
break;
case kCTFontFormatOpenTypeTrueType:
formatName = @"OpenType TrueType";
break;
case kCTFontFormatTrueType:
formatName = @"TrueType";
break;
case kCTFontFormatPostScript:
formatName = @"PostScript";
break;
case kCTFontFormatBitmap:
formatName = @"Bitmap";
break;
case kCTFontFormatUnrecognized:
default:
formatName = @"Unrecognized";
break;
}
NSLog(@"Font: '%@' Format: '%@'", fontName, formatName);
}

关于objective-c - 如何使用 CTFontRef 获取 FontFormat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7161857/

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