Wednesday, February 3, 2016

Objective C Custom Fonts

In this post, Let's discuss on how to add custom fonts to our project and use them in our application.

Here are the sequence of steps to achieve this applying custom fonts.

1. Add your font files to the project.
2. Add those files from project to 'Copy Bundle Resources'.

TARGETS -> Build Phases -> Copy Bundle Resources

3. In the info.plist, in the 'Custom iOS Target Properties' section, add a key name 'Fonts provided by application'. In that key add items by mentioning all of your custom font names as the properties one by one. 

We have successfully added custom fonts to our project. The next step is the interesting step which is applying the custom fonts to our labels in the application.

4. Before applying, we need to know the exact name of the font to apply. For this, we need to do a single iteration of all the fonts to know the exact name of our custom font.



   
 for (NSString *familyName in [UIFont familyNames]) {

        for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {

            NSLog(@"%@", fontName);
        }

    }


The above loop will give you all the font names that available in the system to use in your project. As we have already added our custom fonts to our project, we can see those font names also in the log.


Hope this post is useful. Feel free to comment in case of any queries.