Adding a Custom Font to Your IOS App (Swift 5, Xcode 12, iOS 2021) - iOS Development 2021

In this Xcode tutorial, I discuss the topic of adding a custom font to your iOS app and use it in your app’s interface.

Import font files to Xcode project:


Register Your Font File into Info .plist file :


Open the info.plist file and add a new property named ‘Fonts provided by application’. The raw key of this property is UIAppFonts.When the property is added, the property ‘Fonts provided by application’ key is an Array type and add new items to the array, one for each font file you imported. Names of the items should match imported font file names.


Use Your Custom Font in Source Code:


You can find out what other fonts are already available to use in your app on iOS by running the following code which prints out all the font names.

      for fontFamily in UIFont.familyNames {
            for fontName in UIFont.fontNames(forFamilyName: fontFamily) {
                print("\(fontName)")
            }
        }
---------------

Use Code in helper class:

extension UIFont {
// MARK: Poppins UIFont
static func poppinsBold(size:CGFloat)-> UIFont?{
return UIFont(name: "Poppins-Bold", size: size)
}
static func poppinsLight(size:CGFloat)-> UIFont?{
return UIFont(name: "Poppins-Light", size: size)
}
static func poppinsMedium(size:CGFloat)-> UIFont?{
return UIFont(name: "Poppins-Medium", size: size)
}
static func poppinsSemiBold(size:CGFloat)-> UIFont?{
return UIFont(name: "Poppins-SemiBold", size: size)
}
static func poppinsRegular(size:CGFloat)-> UIFont?{
return UIFont(name: "Poppins-Regular", size: size)
}
// MARK: Karla UIFont
static func karlaSemiBold(size:CGFloat)-> UIFont?{
return UIFont(name: "Karla-SemiBold", size: size)
}
static func karlaBold(size:CGFloat)-> UIFont?{
return UIFont(name: "Karla-Bold", size: size)
}
static func karlaRegular(size:CGFloat)-> UIFont?{
return UIFont(name: "Karla-Regular", size: size)
}
static func karlaLight(size:CGFloat)-> UIFont?{
return UIFont(name: "Karla-Light", size: size)
}
}
view raw UIFont.swift hosted with ❤ by GitHub

Easy to use:

 
@IBOutlet weak var lblName: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    lblName.font = UIFont.poppinsSemiBold(size: 14)
}



Related tutorials:

Post a Comment