How to set Custom font to UILabel or Any Other UIView programmatically (if it is possible) i know how to do it in storyboard but not programmatically thank you
Asked
Active
Viewed 5.3k times
29
-
Possible duplicate of [UILabel - set custom fonts](https://stackoverflow.com/questions/8151262/uilabel-set-custom-fonts) – Vitalii Gozhenko Jun 21 '18 at 22:44
-
Possible duplicate of [Custom Fonts Bug](https://stackoverflow.com/questions/46604829/custom-fonts-bug) – Rocky Jan 20 '19 at 10:39
-
Please see "[MCVE](https://stackoverflow.com/help/minimal-reproducible-example)", and "[Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/a/285557/128421)". – the Tin Man Apr 26 '20 at 18:52
4 Answers
46
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

Shehata Gamal
- 98,760
- 8
- 65
- 87
-
1you have to make sure you add the font to plist , and target memberShip is checked and copied for the font – Shehata Gamal Jun 21 '18 at 22:42
-
yes all done , target membership and i declare it on info.plist, but not working yet – Hakim Douib Jun 21 '18 at 22:44
-
1click 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 – Shehata Gamal Jun 21 '18 at 22:47
-
-
If you need to see the available font styles for your font check this: UIFont.fontNames(forFamilyName: "FontName") – Braden Holt Jul 18 '19 at 00:27
-
1. **Font.otf** select the file. 2. On the right panel (detailed information) find the checkbox for **Target Membership** and make sure it is selected! – J A S K I E R Oct 07 '20 at 16:11
20
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)

Mohammad Razipour
- 3,643
- 3
- 29
- 49
-
1this is not exactly the problem here. you just encapsulated inside an extension. the problem here is how to configure and setup the fonts inside the project. – JBarros35 Jul 01 '20 at 13:39
8
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

J A S K I E R
- 1,976
- 3
- 24
- 42
0
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)!
}
}

Srinivasan_iOS
- 972
- 10
- 12
-
Unexpectedly found nil while unwrapping an Optional value . Getting this crash – Rakesh Lahkar May 04 '22 at 03:30