swift - 为带有扩展名的 UIButton 设置字体

标签 swift

我想让更改整个应用程序的字体和颜色变得容易。我为 UIButton 做了这样的扩展:

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() 时,字体或 fontSize 不会改变。我在这里错过了什么?

最佳答案

您可能使用了错误的字体名称。将此代码添加到您的 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 上的字体名称必须与上面代码中打印的字体名称相同。

关于swift - 为带有扩展名的 UIButton 设置字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57992784/

相关文章:

ios - Swift Firebase 在 child 身上获得值(value)

c - Swift 3 中的不安全可变指针

swift - NSTableView 替代行颜色与 NSArrayController

ios - 从 App 打开 map 返回 Nil 作为自定义点注释的位置标题

swift - LLDB 输出错误 : use of undeclared type '$__lldb_context'

ios - UICollectionView.setContentOffset(...) 导致 NSRangeException

ios - 我如何才能检测到用户是否仍然在 firebase 中有一个身份验证 session ?

ios - 授权用户使用 firebase 无效

ios - CoreData refreshAllObjects() 不会刷新所有对象

swift - Array 在哪里定义 Element 关联类型?