首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用CTFontRef获取FontFormat

如何使用CTFontRef获取FontFormat
EN

Stack Overflow用户
提问于 2011-08-23 21:36:56
回答 1查看 1.2K关注 0票数 2

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-31 10:32:22

CTTypeRef是泛型类型。如果您阅读kCTFontFormatAttribute常量的文档,它们会声明:

与此键关联的值是一个整数,表示为一个CFNumberRef对象,其中包含“Font Format constants”中的一个常量。

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

代码语言:javascript
复制
//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);
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7161857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档