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!

7
  • you have to add that first code into AppDelegate.the swift file inside of ``func application(_ application: UIApplication, didFinishLaunchingWithOptions...``` Commented Feb 12, 2019 at 6:23
  • And if you are using latest swift so use this line UILabel.appearance().font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle(rawValue: "Times New Roman")) Commented Feb 12, 2019 at 6:24
  • 1
    And much better solution is here : stackoverflow.com/questions/8707082/… Commented Feb 12, 2019 at 6: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. Commented Feb 12, 2019 at 6:33
  • @iOSTeam yeah i tried putting that line in the didFinishLaunchingWithOptions func, but still no result. I'll try the answer you suggested Commented Feb 12, 2019 at 7:46

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

is there a way to set this for all the uilabels? Thanks
@brontea you can subclass UILabel and set font in that, Make labels in your app from that subclass.
that's a good idea, thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.