iOS中的动态字体下载需要在每次启动时下载com_apple_MobileAsset_Font.xml吗?

时间:2015-05-10 04:17:02

标签: ios uikit uifont

我想在我的应用中使用动态字体。我发现在Apple的sample code中,每次发布都会下载一个xml,即使我之前已经下载了这个字体。

为什么呢?这个文件的用法是什么?

此示例代码中是否有任何错误,或者如果我想使用此功能,我需要接受此错误。

1 个答案:

答案 0 :(得分:0)

示例代码通常是针对主要开发人员的正确方向,而不是为他们提供功能完善且优化的解决方案。

您应该在下载后保存字体数据,然后在启动时可以使用这样的代码在本地检索它。

+ (void)loadFontAtPath:(NSString *)path {
    NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
    if (!data) {
        NSLog(@"Failed to load font. Data at path is null");
        return;
    }

    CFErrorRef error;
    CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
    CGFontRef font = CGFontCreateWithDataProvider(provider);

    if (!CTFontManagerRegisterGraphicsFont(font, &error)) {
        CFStringRef errorDescription = CFErrorCopyDescription(error);
        NSLog(@"Failed to load font: %@", errorDescription);
        CFRelease(errorDescription);
    }

    CFRelease(font);
    CFRelease(provider);
}

从那里你可以使用[UIFont fontWithName:size:]将其称为正常。

同样从URL请求可能对您有用。 CTFontManagerRegisterFontsForURL