'Swift 4 set custom font programmatically
Solution 1:[1]
You can try
lbl.font = UIFont(name:"FontAwesome",size:15)
the name should be the font name as it is when you install it . not as the file name in your case was Noto Kufi Arabic instead of NotoKufiArabicRegular
click the font and open it with Font Book , then install it after that specify exactly the name shown in the parameter in the line above
Solution 2:[2]
import Foundation
import UIKit
extension UIFont {
public enum OpenSansType: String {
case extraboldItalic = "-ExtraboldItalic"
case semiboldItalic = "-SemiboldItalic"
case semibold = "-Semibold"
case regular = ""
case lightItalic = "Light-Italic"
case light = "-Light"
case italic = "-Italic"
case extraBold = "-Extrabold"
case boldItalic = "-BoldItalic"
case bold = "-Bold"
}
static func OpenSans(_ type: OpenSansType = .regular, size: CGFloat = UIFont.systemFontSize) -> UIFont {
return UIFont(name: "OpenSans\(type.rawValue)", size: size)!
}
var isBold: Bool {
return fontDescriptor.symbolicTraits.contains(.traitBold)
}
var isItalic: Bool {
return fontDescriptor.symbolicTraits.contains(.traitItalic)
}
}
Use:
self.numberLabel.font = UIFont.OpenSans(.bold, size: 20)
Solution 3:[3]
If the previous answer doesn't help, you didn't configure all links correctly at xCode for sure!
- Add this by drag and drop to your files on the left side of the project.
- Add your CUSTOM fonts to the info.plist file.
- Make sure that you've linked all custom fonts you need: "Build Phases" -> "Copy Bundle Resources" -> "+" add your font.
More information you can get here: Medium.com
Solution 4:[4]
Its working fine for Me swift 5
extension UIFont {
public enum fontType: String {
case regular = ""
case kFontBlackItalic = "Montserrat-BlackItalic"
case kFontExtraBoldItalic = "Montserrat-ExtraBoldItalic"
case kFontBoldItalic = "Montserrat-BoldItalic"
case kFontSemiBoldItalic = "Montserrat-SemiBoldItalic"
case kFontMediumItalic = "Montserrat-MediumItalic"
case kFontItalic = "Montserrat-Italic"
case kFontLightItalic = "Montserrat-LightItalic"
case kFontBlack = "Montserrat-Black"
case kFontExtraLightItalic = "Montserrat-ExtraLightItalic"
case kFontThinItalic = "Montserrat-ThinItalic"
case kFontExtraBold = "Montserrat-ExtraBold"
case kFontBold = "Montserrat-Bold"
case kFontSemiBold = "Montserrat-SemiBold"
case kFontMedium = "Montserrat-Medium"
case kFontRegular = "Montserrat-Regular"
case kFontLight = "Montserrat-Light"
case kFontExtraLight = "Montserrat-ExtraLight"
case kFontThin = "Montserrat-Thin"
}
static func setFont(_ type: fontType = .regular, size: CGFloat = UIFont.systemFontSize) -> UIFont {
return UIFont(name: type.rawValue, size: size)!
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Mohammad Razipour |
| Solution 3 | J A S K I E R |
| Solution 4 | Srinivasan_iOS |

