在我的 iOS 应用程序中,我使用从文件动态加载的自定义字体。
要在代码中使用它们,我需要知道加载的字体系列。那么,有什么办法可以做到吗?
更新:
我无法以某种方式对字体系列进行硬编码,因为我的应用程序从服务器加载它。当然,有一种方法可以在服务器响应中传递字体系列,但现在我正在寻找一个不会影响服务器的更好版本(不需要更改它)。
在我的 iOS 应用程序中,我使用从文件动态加载的自定义字体。
要在代码中使用它们,我需要知道加载的字体系列。那么,有什么办法可以做到吗?
更新:
我无法以某种方式对字体系列进行硬编码,因为我的应用程序从服务器加载它。当然,有一种方法可以在服务器响应中传递字体系列,但现在我正在寻找一个不会影响服务器的更好版本(不需要更改它)。
我做的字体加载...
NSData *fontData = [NSData dataWithContentsOfFile:filePath];
if (fontData) {
    // create font from loaded data and get it's postscript name
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithCFData((CFDataRef)fontData);
    CGFontRef loadedFont = CGFontCreateWithDataProvider(fontDataProvider);
    NSString *postScriptName = (NSString *)CGFontCopyPostScriptName(loadedFont);
    // if the font with same postscript name wasn't already registered - do it
    if (postScriptName && ![UIFont fontWithName:postScriptName size:10]) {
        NSError *error;
        if (!CTFontManagerRegisterGraphicsFont(loadedFont, (CFErrorRef *)&error)) {
            NSLog(@"Can't load font: %@", error);
        }
    }
    CGFontRelease(loadedFont);
    CGDataProviderRelease(fontDataProvider);
}
    我从堆栈溢出的某个地方得到了这段代码。希望能帮助到你。它列出了所有加载的字体名称...
NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily) {
   NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
   fontNames = [[NSArray alloc] initWithArray:
   [UIFont fontNamesForFamilyName:
   [familyNames objectAtIndex:indFamily]]];
   for (indFont=0; indFont<[fontNames count]; ++indFont) {
      NSLog(@"    Font name: %@", [fontNames objectAtIndex:indFont]);
   }
}