Have any Question?

You can ask below or enter what you are looking for!

How to add custom font to iOS app in Xcode

You can add True Type Font (.ttf) or Open Type Font (.otf) to your app and use it the same way you use font provided by iOS.

Add your font file to your Xcode project

From menu bar select File->Add Files to “project_name” or just drag font files to your project

custom font Xcode

And make sure your font is a target member of your app in “Target membership” section

custom font in Xcode



Register your font with iOS

iOS need to know about the new fonts, so open info.plist and add that key “Fonts provided by application” (its raw name is UIAppFonts) to your file.

Xcode will create an array for that key. Add every new font file name (including the extension) as an item to that array.

register custom font with iOS

Using your custom font in Interface Builder

Now if you select a label for example from Interface Builder and select Font -> custom from “Attributes inspector” you will find your custom font in the list

custom font in iOS

Using custom font in code

To use your font in code you must first know its name (not the font file name)

Run this for-loop anywhere in your app to print a list of available fonts

for family in UIFont.familyNames.sorted() {
       let names = UIFont.fontNames(forFamilyName: family)
       print("Family: \(family) Font names: \(names)")
}

Result:

print list of available fonts in Xcode

Once you located your font name we can create instance of UIFont class to use it:

if let myFont = UIFont.init(name: "FuturaCE-Book", size: 12) {
      lblTitle.font = myFont
}

and don’t forget to remove the for-loop code

Learn how to create custom controls in Interface Builder using @IBDesignable and @IBInspectable

Leave a Reply

Your email address will not be published. Required fields are marked *