1

I'm trying to change the font throughout an iOS app in Swift. I'm currently using:

 var size:CGFloat = 16

 UILabel.appearance().font = UIFont(name: "Gudea", size: size)

Which changes every UILabel in my app to be the correct font. However, all labels are changed to size 16 despite different sizes assigned in the Main.Storyboard.

Is there a way to assign a font to every UILabel without changing the sizes assigned in Main.Storyboard?

or, perhaps,

Is there a better way to change the font used throughout the app?

Thanks!

3
  • The answer to your first question is no (unless maybe you write a script to run through the storyboard). I'd suggest creating some sort of global constant or UIFont category and start using that when you assign fonts programmatically. Commented Jun 4, 2015 at 23:59
  • @AdamPro13 that makes sense. perhaps the best solution is to simply use my current solution and override with an abstracted UIFont constant for headers and the like. Commented Jun 5, 2015 at 0:10
  • Yep, I tend to create a UIFont category & create class methods like [UIFont standardGudea]. This is also great when you have a bunch of colors that you use throughout your app. Commented Jun 5, 2015 at 0:12

1 Answer 1

2

My solution was to extend UIFont and set my labels programatically.

UIFontExtension.swift:

extension UIFont {

    class func myFontName() -> String { return "Gudea" }
    class func myBoldFontName() -> String { return "Gudea-Bold" }

    class func myNormalFont() -> UIFont {
        return UIFont(name: UIFont.myFontName(), size: 12)!
    }

    class func mySmallBoldFont() -> UIFont {
        return UIFont(name: UIFont.myBoldFontName(), size: 10)!
    }
}

MyViewController.swift:

myLabel.font = UIFont.myNormalFont()

mySmallLabel.font = UIFont.mySmallBoldFont()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.