How to use Custom Fonts
How to use custom fonts
Download and Add Font files
In your Xcode project, Create a Font folder and drag and drop all the font files (.ttf or .otf) here in this folder. While moving files in xcode make sure to select Copy Items if needed
and Select your app in Add to target
.
Make sure you add the following type of files:
<FontName>
-Regular.ttf
<FontName>
-Bold.ttf
<FontName>
-SemiBold.ttf
<FontName>
-ExtraBold.ttf
<FontName>
-Light.ttf
<FontName>
-BoldItalic.ttf
<FontName>
-Italic.ttf
Add the Fonts in Info.Plist
Inside your xcode project's Info.plist, add the font files by including the following code.
<key>UIAppFonts</key>
<array>
<string><FontName>-Regular.ttf</string>
<string><FontName>-Bold.ttf</string>
<string><FontName>-SemiBold.ttf</string>
<string><FontName>-ExtraBold.ttf</string>
<string><FontName>-Light.ttf</string>
<string><FontName>-BoldItalic.ttf</string>
<string><FontName>-Italic.ttf</string>
</array>
For example, if you add Mulish Font, the info.plist should look like this (7 items)
Recommended Initialization
It is recommended to initialize the custom Fonts at the time of app launch. In your Xcode project, initialize the fonts in the AppDelegate
under the following function:
func registerFont(_ font: String, _ bundle: Bundle) {
_ = UIFont()
guard
let fontPath = bundle.path(forResource: font, ofType: "ttf"),
let dataFont = NSData(contentsOfFile: fontPath),
let provider = CGDataProvider(data: dataFont)
else { return }
let fontRef = CGFont(provider)
if let fontRef = fontRef {
CTFontManagerRegisterGraphicsFont(fontRef, nil)
}
}
Register your Fonts
Register your fonts in the AppDelegate
under the following function:
public func loadCustomFonts(){
let bundle = Bundle.main
self.registerFont("<FontName>-Regular", bundle)
self.registerFont("<FontName>-Bold", bundle)
self.registerFont("<FontName>-SemiBold", bundle)
self.registerFont("<FontName>-ExtraBold", bundle)
self.registerFont("<FontName>-Light", bundle)
self.registerFont("<FontName>-BoldItalic", bundle)
self.registerFont("<FontName>-Italic", bundle)
}