【发布时间】:2018-04-17 09:32:54
【问题描述】:
我正在像这样更改 UIButton 标题的字体:
self.titleLabel?.font = UIFont(name: "SF UI Display", size: 30)
然后我该如何更改权重(我认为 XCode 将其称为“样式”)?它目前默认为中等,我需要它是常规的。
【问题讨论】:
-
包括了Answer中的步骤
我正在像这样更改 UIButton 标题的字体:
self.titleLabel?.font = UIFont(name: "SF UI Display", size: 30)
然后我该如何更改权重(我认为 XCode 将其称为“样式”)?它目前默认为中等,我需要它是常规的。
【问题讨论】:
您可以下载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"
在使用字体名称后 - 并输入您需要的样式名称,确保 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 等。
【讨论】:
试试这个代码。它对你有用
self.titleLabel?.font = UIFont(name: "SFUIDisplay-Regular", size: 30)
【讨论】: