简体   繁体   中英

How do I set the weight of the font of a titleLabel of a UIButton?

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.

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:

在此处输入图片说明

试试这个代码。它为您工作

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

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.

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