3

i already know how to load a custom font to my project in iPhone App from here I want to ask if there is a way to do this from code? My problem is that I have a resource folder in my app, i have a font file name, lets call it "myfont.ttf".

I want to grab a ttf file and put it to plist file from code, and what's more i want to know the display name for fontWithName:size: method. There is a way to achieve this?

Jakub
  • 13,712
  • 17
  • 82
  • 139

3 Answers3

14

This is an older question but here is a way to do this anyway in case someone else comes across this.


+ (void)loadFontAtPath:(NSString*)path{
    NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
    if(data == nil){
        NSLog(@"Failed to load font. Data at path is null");
        return;
    }
    CFErrorRef error;
    CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
    CGFontRef font = CGFontCreateWithDataProvider(provider);
    if(!CTFontManagerRegisterGraphicsFont(font, &error)){
        CFStringRef errorDescription = CFErrorCopyDescription(error);
        NSLog(@"Failed to load font: %@", errorDescription);
        CFRelease(errorDescription);
    }
    CFRelease(font);
    CFRelease(provider);
}

This will load the font at the path specified at runtime then you can use it in the same way as normal without adding it to the plist.

cnotethegr8
  • 7,342
  • 8
  • 68
  • 104
pixelrevision
  • 364
  • 3
  • 12
  • This worked, it applied font to Label, but it did not registerd. – Durgaprasad May 04 '13 at 05:24
  • @pixelrevision whether CTFontManagerRegisterGraphicsFont will register the font permanently to the device – vignesh kumar May 24 '13 at 08:39
  • CGFontRef font = CGFontCreateWithDataProvider(provider); application hangs on this line for forever. (iOS 10.2) – dev gr Jan 04 '17 at 11:31
  • 1
    Found solution at Found the solution at http://stackoverflow.com/questions/24900979/cgfontcreatewithdataprovider-hangs-in-airplane-mode – dev gr Jan 04 '17 at 11:43
4

Yes you can. But you've to work a lot with CoreText and/or CoreGraphics.

There's a nice class from Zynga that could help you in doing this: https://github.com/zynga/FontLabel

The example project shows how to load .ttf files from the bundle without using the .plist and use these fonts inside the application.

The code is valid and is a good point from start.

Edit: The previous approach uses CoreGraphics, that is good, but use Core Text is much better. I found an interesting answer to this question: How can you load a font (TTF) from a file using Core Text?

If you don't have experience with CoreText framework, please read the official introduction inside the Apple documentation.

Community
  • 1
  • 1
bontoJR
  • 6,995
  • 1
  • 26
  • 40
2

IF you are downloading a TTF file then you can do following to register your custom fonts with iOS Font Manager, this piece of code also takes care of TTF file updates (font updates):

    +(void)registerFontsAtPath:(NSString *)ttfFilePath
    {
        NSFileManager * fileManager = [NSFileManager defaultManager];

        if ([fileManager fileExistsAtPath:ttfFilePath] == YES)
        {
            [UIFont familyNames];//This is here for a bug where font registration API hangs for forever.

            //In case of TTF file update : Fonts are already registered, first de-register them from Font Manager
            CFErrorRef cfDe_RegisterError;
           bool fontsDeregistered = CTFontManagerUnregisterFontsForURL((__bridge CFURLRef)[NSURL fileURLWithPath:ttfFilePath], kCTFontManagerScopeNone, &cfDe_RegisterError);


            //finally register the fonts with Font Manager,
            CFErrorRef cfRegisterError;
            bool fontsRegistered= CTFontManagerRegisterFontsForURL((__bridge CFURLRef)[NSURL fileURLWithPath:ttfFilePath], kCTFontManagerScopeNone, &cfRegisterError);
         }
     }

You can check for booleans and errors for registration and de-registration status.

dev gr
  • 2,409
  • 1
  • 21
  • 33