29

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

this what i did enter image description here

Hakim Douib
  • 495
  • 1
  • 6
  • 13
  • 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 Answers4

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
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
  • 1
    this 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!

  1. Add this by drag and drop to your files on the left side of the project.
  2. Add your CUSTOM fonts to the info.plist file.
  3. 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