How to set a custom font for entire iOS app without specifying size

Viewed 30313

I'm trying to apply a custom font throughout my iOS app. I found that I could use:

 [[UILabel appearance] setFont:[UIFont fontWithName:@"Proxima Nova" size:17.0]];
  • To set the default font and size for all UILabels. However, not all my UILabels share the same font size.

  • In Set a default font for whole iOS app?, someone had the same concern, and was told to set the size parameter to 0.0 to only set the font and not font size.

  • When I tried doing this, all the UILabel text in my app disappeared (because evidently iOS took the 0.0 font size literally).

Any suggestions as to how I can universally set a font but not size? Thanks a lot!

7 Answers

Swift3 https://gist.github.com/dimohamdy/c47992e14c1a3ee3315c06e2480e121e

you can't set appearance for label font that make me create other value of default font and when i set the new font i get only the name of new font and old size of label and create other font and set this font

    //when you want to set font for all labels in Application
    UILabel.appearance().defaultFont =  UIFont.systemFont(ofSize: 15/*Any Value*/, weight: UIFontWeightThin)

extension UILabel{
    dynamic var defaultFont: UIFont? {
        get { return self.font }
        set {
          //get old size of lable font
          let sizeOfOldFont = self.font.pointSize 
          //get new name  of font
          let fontNameOfNewFont = newValue?.fontName
          self.font = UIFont(name: fontNameOfNewFont!, size: sizeOfOldFont)
        }
    }
     }
Related