0
votes

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!

1
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.AdamPro13
@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.olympia
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.AdamPro13

1 Answers

2
votes

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()