0

I'm changing the font of the title of a UIButton like this:

self.titleLabel?.font = UIFont(name: "SF UI Display", size: 30)

How do I then change the weight (I think XCode calls it "style")? It is currently defaulting to medium and I need it to be regular.

Sean
  • 629
  • 8
  • 24

3 Answers3

2

You can Download the The Regular Font

Try printing the Font Name:

 func printFonts() {
    let fontFamilyNames = UIFont.familyNames
    for familyName in fontFamilyNames {
        print("------------------------------")
        print("Font Family Name = [\(familyName)]")
        let names = UIFont.fontNames(forFamilyName: familyName)
        print("Font Names = [\(names)]")
    }
}

and then set it as follows:

self.titleLabel?.font = UIFont(name: "pass the name of downloaded file", size: 30) //Pass the name of downloaded file here

Note:- After Downloading and Drag and Dropping into your Project, Do not forget to check the Target Membership is ticked or not, if not then tick it or it will not work for you

Edit For setting appropriate name

Remove the existing font file by moving into trash and then

Change the file name after downloading to SFUIDisplayRegular.otf

the tick the target membership

self.titleLabel?.font =  UIFont(name: "SFUIDisplayRegular", size: 20)

And it will work for you,

Cheers!

See the Output:

enter image description here

Abhirajsinh Thakore
  • 1,806
  • 2
  • 13
  • 23
  • Thanks... I tried this but can't get it to pick up. I can get the main font to work but as soon as I use `"SF UI Display Regular"` it doesn't pick up at all. I've checked that Target Membership is correct for the app too. I've also tried things like `"Times New Roman"` (works) but `"Times New Roman Italic"` doesn't work. – Sean Apr 17 '18 at 09:51
  • lemme integrate this in my project – Abhirajsinh Thakore Apr 17 '18 at 09:55
  • Thanks! Yes. it was: `"SFUIDisplay-Regular"` – Sean Apr 17 '18 at 10:05
  • Same for "Times New Roman Italic" try removing the space and then insert it once again and may be it works for you, Cheers – Abhirajsinh Thakore Apr 17 '18 at 10:07
1

Try this code.it's working for you

self.titleLabel?.font = UIFont(name: "SFUIDisplay-Regular", size: 30)
1

After name of font use - and type the style name you need make sure that font file with that style name exist or support in xcode project.

struct Fonts {
        static let regular12 = UIFont.init(name: "OpenSans", size: 12) ?? UIFont.systemFont(ofSize: 12)
        static let regular14 = UIFont.init(name: "OpenSans", size: 14) ?? UIFont.systemFont(ofSize: 14)
        static let regular16 = UIFont.init(name: "OpenSans", size: 16) ?? UIFont.systemFont(ofSize: 16)
        static let regular18 = UIFont.init(name: "OpenSans", size: 18) ?? UIFont.systemFont(ofSize: 18)
        static let semiBold12 = UIFont.init(name: "OpenSans-Semibold", size: 12) ?? UIFont.systemFont(ofSize: 12)
        static let semiBold14 = UIFont.init(name: "OpenSans-Semibold", size: 14) ?? UIFont.systemFont(ofSize: 14)
        static let semiBold16 = UIFont.init(name: "OpenSans-Semibold", size: 16) ?? UIFont.systemFont(ofSize: 16)
        static let bold12 = UIFont.init(name: "OpenSans-Bold", size: 12) ?? UIFont.systemFont(ofSize: 12)
        static let bold14 = UIFont.init(name: "OpenSans-Bold", size: 14) ?? UIFont.systemFont(ofSize: 14)
        static let bold16 = UIFont.init(name: "OpenSans-Bold", size: 16) ?? UIFont.systemFont(ofSize: 16)
        static let regular20 = UIFont.init(name: "OpenSans", size: 20) ?? UIFont.systemFont(ofSize: 20)
}

File Names that i have used in project for font are following:- Google Fonts - Open Sans Bold Italic.ttf, Google Fonts - Open Sans Bold.ttf, Google Fonts - Open Sans Semibold.ttf etc.

Sand'sHell811
  • 358
  • 3
  • 15