简体   繁体   中英

How to change font for iOS app in Swift with dynamic size?

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM