Ghostboard pixel

Using Custom Fonts

Using Custom Fonts

Out of the box iOS provides you a rich set of fonts. But sometimes there is the need to add another font to your project. For example, this could be one of the Google Fonts, which are open source.  In this post we look how you can add a custom font to your project and how you can use it.

Adding a custom font

  1. Get the font as a .ttf file. Don’t forget to read the license agreement! You must be sure that you are allowed to use the font. There are almost always some restrictions. For example, sometimes you have to publish the license agreement in your app’s imprint.
  2. Add the .ttf file to your project.
    Bildschirmfoto 2015-07-15 um 18.40.08
  3. If you want to use the font not only in the interface builder but also in your code, you have to add the .ttf file to your target.
    Bildschirmfoto 2015-07-15 um 18.57.23
  4. Go to your plist and add the key “Fonts provided by application”. Then add “Item 0” and specify the name of your font file as the key.
    Bildschirmfoto 2015-07-15 um 18.45.18

Using a custom font

After you have added a custom font to your project, you can use it right away in the interface builder:
Bildschirmfoto 2015-07-15 um 18.45.07

If you want to use the custom font in your code, you first need to know the “PostScript-Name” for the font. You can find it if you double click the .ttf file in the finder:

Bildschirmfoto 2015-07-15 um 18.47.18

Now you can change for example the font of a label with the following method call:

aLabel.font = UIFont(name: "OpenSans", size: 12)