Swift 为带有扩展名的UIButton设置字体
我想让整个应用程序中更改字体和颜色变得容易。我对Swift 为带有扩展名的UIButton设置字体,swift,Swift,我想让整个应用程序中更改字体和颜色变得容易。我对ui按钮做了如下扩展: extension UIButton { func cancelAndSaveButtons() { backgroundColor = Theme.tintColor layer.shadowOpacity = 0.25 layer.shadowOffset = CGSize(width: 0, height: 10) layer.shadowRa
ui按钮做了如下扩展:
extension UIButton {
func cancelAndSaveButtons() {
backgroundColor = Theme.tintColor
layer.shadowOpacity = 0.25
layer.shadowOffset = CGSize(width: 0, height: 10)
layer.shadowRadius = 5
layer.cornerRadius = frame.height / 2
setTitleColor(Theme.mainFontColor, for: .normal)
titleLabel?.font = UIFont(name: Theme.mainButtonFont, size: 25)
}
}
我有一个主题为的课程
:
class Theme {
static let mainFontName = "BrushScriptMT"
static let mainButtonFont = "FredokaOne"
static let accentColor = UIColor(named: "Accent")
static let backgroundColor = UIColor(named: "Background")
static let tintColor = UIColor(named: "Tint")
static let mainFontColor = UIColor(named: "MainFontColor")
}
但是,在viewDidLoad
中调用myButton.cancelAndSaveButtons()
时,字体或字体大小不会改变。我错过了什么 您可能使用了错误的字体名称。将此代码添加到您的AppDelegate
,并检查您使用的名称是否正确:
for fontFamilyName in UIFont.familyNames {
print("family: \(fontFamilyName)\n")
for fontName in UIFont.fontNames(forFamilyName: fontFamilyName) {
print("font: \(fontName)")
}
}
编辑:
打印字体更酷:
dump(UIFont.familyNames)
此外,请仔细检查是否已导入字体文件并将其添加到plist中。plist上的字体名称必须与上面代码上的字体名称相同。这是一个拼写错误。。谢谢