Thomas Denney

Automatically register custom fonts on iOS

In order to add a custom font file to your iOS application the most popular method is to usually use UIAppFonts key in Info.plist, however this method will allow you to automatically register fonts by going through Core Text:

+ (void)registerFonts {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSArray * paths = [[NSBundle mainBundle] pathsForResourcesOfType:@"ttf" inDirectory:@""];
        for (NSString * path in paths) {
            NSURL * url = [NSURL fileURLWithPath:path];
            CFErrorRef error;
            CTFontManagerRegisterFontsForURL((__bridge CFURLRef)url, kCTFontManagerScopeNone, &error);
            error = nil;
        }
    });
}

Obviously if you’ve only got one custom font that you are going to be using in your application then there is little point in using this method, however it is a neat time saver if you are working on a project where you are testing lots of fonts or if you are shipping with lots of fonts.