Last updated on: May 27, 2023
Today, I’m going to show you how to add custom fonts in your iOS app quickly.
In this example, we are going to add 2 fonts, Mallent (MallentRegular.ttf) and Great Wishes (Best.otf), in our iOS project.
Contents
Adding Custom Fonts to the project
First, create a folder with a name Fonts in your project. Go to your project and right-click > New Group
Drag and Drop your font files in the new folder and choose the checkboxes for Copy items if needed and Add to targets.
Then press Finish.
After adding the fonts, the Fonts folder will look like this:
Now, go to Info.plist and hover your mouse over the Information Property List and press the + button and type Fonts provided by application and press Enter
Press the arrow next to Fonts provided by application and add the font file names.
Using Custom Fonts in the app
Go to the UILabel you want to use the custom font and add it:
class ViewController: UIViewController {
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
myLabel.font = UIFont(name: "MallentRegular", size: 50)
myLabel.text = "Text"
}
}
Code language: Swift (swift)
It didn’t work! What I’m doing wrong?
Sometimes the font file name differs from the font name. In this example, we have a font file name Best.otf, but the name of the font is not Best.
If we try to add the font like that:
class ViewController: UIViewController {
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
myLabel.font = UIFont(name: "Best", size: 50)
myLabel.text = "Text"
}
}
Code language: Swift (swift)
Won’t work.
We need to use the name of the font, not the name of the font file.
To find the name of the font, go to your font file and Right-Click > Get Info
So the font name is GreatWishes.
So the right way is:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
myLabel.font = UIFont(name: "GreatWishes", size: 50)
myLabel.text = "Text"
}
}
Code language: Swift (swift)
You can find the final project here
If you have any questions, please feel free to leave a comment below