0

I want to find out the PostScript Name of a .ttf file which I have copied to Documents Directory ... So that I can set the font to a label programmatically without changing the .plist file.

I know this code :

  for (NSString *fontFamilyName in [UIFont familyNames]) {
    for (NSString *fontName in [UIFont fontNamesForFamilyName:fontFamilyName]) {
        NSLog(@"Family: %@    Font: %@", fontFamilyName, fontName);
    }
}

But I want to find PostScript name for only one file example : segoe.ttf

Obviously there will be different font files which will be copied to Documents Directory and I would like to get the PostScript Name for every single file only and not like the one from the code mentioned above. Thank You ...

2
  • Open FontBook.app on your macOSX, install the font, and check it there. You can't do what you want to do. There is no "What's the font that's are not here by default method".
    – Larme
    Commented Jun 19, 2017 at 8:32
  • I know how to do that Sir. The point is I want to make it work programmatically inside the app. Commented Jun 19, 2017 at 8:33

1 Answer 1

1

So I got it using this code :

- (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;

Not the answer you're looking for? Browse other questions tagged or ask your own question.