如何设置 UIButton 的 titleLabel 的字体粗细?

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

我正在像这样更改 UIButton 的标题字体:

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

我该如何更改权重(我认为 XCode 称之为 "style")?它目前默认为中等,我需要它是常规的。

您可以下载 The Regular Font

尝试打印字体名称:

 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)]")
    }
}

然后设置如下:

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

注意:- 下载并拖放到您的项目后,不要忘记检查 Target Membership 是否被勾选,如果没有则勾选它或它对你不起作用

编辑用于设置适当的名称

通过移至回收站删除现有字体文件,然后

下载后修改文件名为SFUIDisplayRegular.otf

勾选目标会员

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

它会为你工作,

干杯!

查看输出:

试试这个code.it对你有用

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

在字体名称后使用 - 并键入您需要的样式名称,确保 xcode 项目中存在或支持具有该样式名称的字体文件。

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

我在项目中使用的字体文件名如下:- Google 字体 - Open Sans Bold Italic.ttf, Google 字体 - Open Sans Bold.ttf, Google 字体 - Open Sans Semibold.ttf 等