应用程序提供的字体列表(iOS) [英] List of Fonts Provided by Application (iOS)

查看:229
本文介绍了应用程序提供的字体列表(iOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有人知道如何从Xcode中的info.plist文件中的'应用程序提供的字体'键获取自定义字体列表?
谢谢

解决方案

以下代码从Info.plist中读取自定义字体文件列表
并从字体文件中提取完整的字体名称。
(部分代码复制自 https://stackoverflow.com/a/17519740/1187415 $

Objective-C

  NSDictionary * infoDict = [[NSBundle mainBundle] infoDictionary]; 
NSArray * fontFiles = [infoDict objectForKey:@UIAppFonts];
$ b $(字体文件中的NSString * fontFile){
NSLog(@文件名:%@,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);

$ / code>

Swift 3相当于:

 如果让infoDict = Bundle.main.infoDictionary,
让fontFiles = infoDict [UIAppFonts]为? fontFiles中fontFile的$ {
{
print(file name,fontFile)
if url = Bundle.main.url(forResource:fontFile,withExtension:nil),
让fontData = NSData(contentsOf:url),
let fontDataProvider = CGDataProvider(data:fontData){
let loadedFont = CGFont(fontDataProvider)
if fullName = loadedFont.fullName {
print(font name,fullName)
}
}
}
}


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

解决方案

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)
            }
        }
    }
}

这篇关于应用程序提供的字体列表(iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆