List of Fonts Provided by Application (iOS)

拈花ヽ惹草 提交于 2019-12-30 08:30:46

问题


does anyone know how to get a list of custom fonts from the 'Fonts provided by application' key in the info.plist file in Xcode? Thanks


回答1:


The following code reads the list of custom font files from the Info.plist, and extracts the full font name from the font file. (Parts of the code is copied from https://stackoverflow.com/a/17519740/1187415 with small modifications and ARC adjustments).

Objective-C

NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
NSArray* fontFiles = [infoDict objectForKey:@"UIAppFonts"];

for (NSString *fontFile in fontFiles) {
    NSLog(@"file name: %@", fontFile);
    NSURL *url = [[NSBundle mainBundle] URLForResource:fontFile withExtension:NULL];
    NSData *fontData = [NSData dataWithContentsOfURL:url];
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData);
    CGFontRef loadedFont = CGFontCreateWithDataProvider(fontDataProvider);
    NSString *fullName = CFBridgingRelease(CGFontCopyFullName(loadedFont));
    CGFontRelease(loadedFont);
    CGDataProviderRelease(fontDataProvider);
    NSLog(@"font name: %@", fullName);
}

Swift 3 equivalent:

if let infoDict = Bundle.main.infoDictionary,
    let fontFiles = infoDict["UIAppFonts"] as? [String] {
    for fontFile in fontFiles {
        print("file name", fontFile)
        if let url = Bundle.main.url(forResource: fontFile, withExtension: nil),
            let fontData = NSData(contentsOf: url), 
            let fontDataProvider = CGDataProvider(data: fontData) {
            let loadedFont = CGFont(fontDataProvider)
            if let fullName = loadedFont.fullName {
                print("font name", fullName)
            }
        }
    }
}



回答2:


You can add your costume font http://www.danielhanly.com/blog/tutorial/including-custom-fonts-in-ios/

But, I don't, know how to get this list, sorry. But, may be you can look on it in IB, in UILabel attributes.

来源:https://stackoverflow.com/questions/18298713/list-of-fonts-provided-by-application-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!