有没有人知道如何从Xcode的info.plist文件中的“应用程序提供的字体”键获取自定义字体列表? 感谢
答案 0 :(得分:6)
以下代码从Info.plist中读取自定义字体文件列表, 并从字体文件中提取完整的字体名称。 (部分代码是从https://stackoverflow.com/a/17519740/1187415复制的 经过小修改和ARC调整。)
目标-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等价物:
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)
}
}
}
}
答案 1 :(得分:-1)
您可以添加服装字体http://www.danielhanly.com/blog/tutorial/including-custom-fonts-in-ios/ 但是,我不知道如何获得此列表,抱歉。 但是,您可以在IB中查看UILabel属性。