【问题标题】:How do I set the weight of the font of a titleLabel of a UIButton?如何设置 UIButton 的 titleLabel 字体的权重?
【发布时间】:2018-04-17 09:32:54
【问题描述】:

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

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

然后我该如何更改权重(我认为 XCode 将其称为“样式”)?它目前默认为中等,我需要它是常规的。

【问题讨论】:

标签: swift uibutton uifont


【解决方案1】:

您可以下载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)

它会为你工作,

干杯!

查看输出:

【讨论】:

  • 谢谢...我试过了,但无法获取。我可以让主字体工作,但是一旦我使用"SF UI Display Regular",它就根本无法使用。我已经检查过 Target Membership 是否适用于该应用程序。我也尝试过"Times New Roman"(有效)之类的方法,但"Times New Roman Italic" 无效。
  • 让我把它集成到我的项目中
  • 谢谢!是的。它是:"SFUIDisplay-Regular"
  • “Times New Roman Italic”也一样,尝试删除空格,然后再次插入,可能对你有用,干杯
【解决方案2】:

在使用字体名称后 - 并输入您需要的样式名称,确保 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 Fonts - Open Sans Bold Italic.ttf、Google Fonts - Open Sans Bold.ttf、Google Fonts - Open Sans Semibold.ttf 等。

【讨论】:

    【解决方案3】:

    试试这个代码。它对你有用

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

    【讨论】:

      猜你喜欢
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多