我想查找我已复制到Documents Directory的.ttf文件的PostScript名称...这样我就可以在不更改.plist文件的情况下以编程方式将字体设置为标签。
我知道这段代码:
for (NSString *fontFamilyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:fontFamilyName]) {
NSLog(@"Family: %@ Font: %@", fontFamilyName, fontName);
}
}
但是我想找到一个文件示例的PostScript名称:segoe.ttf
显然会有不同的字体文件被复制到Documents Directory,我想只为每个文件获取PostScript名称,而不是像上面提到的代码那样。谢谢......
答案 0 :(得分:0)
所以我使用这段代码得到它:
- (NSString *)realNameOfFont:(NSString *)fullFontName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docFilePath = [documentsDirectory stringByAppendingPathComponent:fullFontName];
NSData *data = [NSData dataWithContentsOfFile:docFilePath];
if (data == nil) {
NSLog(@"Failed to load font. Data at path %@ is null", fullFontName);
return nil;
}
CFErrorRef errorRef;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
CGFontRef font = CGFontCreateWithDataProvider(provider);
NSString *realFontName = (__bridge NSString *)CGFontCopyPostScriptName(font);
if (!CTFontManagerRegisterGraphicsFont(font, &errorRef)) {
NSError *error = (__bridge NSError *)errorRef;
if (error.code != kCTFontManagerErrorAlreadyRegistered) {
NSLog(@"Failed to load font: %@", error);
}
}
NSLog(@"PostScript Font Name:%@", realFontName);
CFRelease(font);
CFRelease(provider);
return realFontName;