有一个旧的碳码使用FMGetFontFormat来确定字体是否为"True Type“。由于API已被弃用,有没有方法可以使用CTFontRef找到相同的API。我使用了CTFontCopyAttribute,但它返回CTTypeRef,并且我无法获取值?有什么建议吗?
发布于 2011-08-31 10:32:22
CTTypeRef是泛型类型。如果您阅读kCTFontFormatAttribute常量的文档,它们会声明:
与此键关联的值是一个整数,表示为一个CFNumberRef对象,其中包含“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);
}https://stackoverflow.com/questions/7161857
复制相似问题