1

I have tried some answers on here to set a custom font for a whole app, but none seem to be working in Xcode 10.1. I was wondering if anyone has some advice?

Here is what I have tried so far;
Using custom font for entire iOS app swift

Set a default font for whole iOS app?

Here is a code sample: (This is still giving me system font)

override func viewDidLoad() {
    super.viewDidLoad()
    UILabel.appearance().font = UIFont(name: "Times New Roman", size: 17)

    let label1 = UILabel(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
    label1.text = "Hello"
    view.addSubview(label1)
}

Thanks!

brontea
  • 555
  • 4
  • 14
  • you have to add that first code into AppDelegate.the swift file inside of ``func application(_ application: UIApplication, didFinishLaunchingWithOptions...``` –  Feb 12 '19 at 06:23
  • And if you are using latest swift so use this line UILabel.appearance().font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle(rawValue: "Times New Roman")) –  Feb 12 '19 at 06:24
  • 1
    And much better solution is here : https://stackoverflow.com/questions/8707082/set-a-default-font-for-whole-ios-app/40484460#40484460 –  Feb 12 '19 at 06:26
  • @brontea cross check font type face name, correct one you need to give here UIFont(name: "Times New Roman", size: 17).I hope you have added font in plist. – Sakshi Feb 12 '19 at 06:33
  • @iOSTeam yeah i tried putting that line in the didFinishLaunchingWithOptions func, but still no result. I'll try the answer you suggested – brontea Feb 12 '19 at 07:46
  • @Sakshi i did double check, thanks for the thought – brontea Feb 12 '19 at 07:48
  • @iOSTeam that answer was really helpful, thanks! – brontea Feb 12 '19 at 08:00

1 Answers1

0

you may do using an extension of UIFont

extension UIFont {

   public enum FontWeight {
      //cases are just font family like bold, light, italic
      case .light
   }


   func appFont(weight : FontWeight, size : CGFloat) -> UIFont {
      var fontName = "Times New Roman-"
      switch weight {
         case .light {
             fontName = fontName + "light" // add your family name here
         }
      }
      return UIFont(name: fontName, size: size)!
    }
}

in case of a label

label.font = UIFont.appFont(weight : .light, size : 17)
Ankur Lahiry
  • 2,253
  • 1
  • 15
  • 25