代码之家  ›  专栏  ›  技术社区  ›  PerNil

为带扩展名的uibutton设置字体

  •  0
  • PerNil  · 技术社区  · 5 年前

    我想在我的应用程序中轻松地更改字体和颜色。我延长了 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 主题:

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

    但是,当我打电话给 myButton.cancelAndSaveButtons() 在里面 viewDidLoad 字体或字体大小不变。我错过了什么?

    1 回复  |  直到 5 年前
        1
  •  1
  •   summerfinn3    5 年前

    您可能使用了错误的字体名。将此代码添加到 AppDelegate 并检查您是否使用了正确的名称:

    for fontFamilyName in UIFont.familyNames {
            print("family: \(fontFamilyName)\n")
    
            for fontName in UIFont.fontNames(forFamilyName: fontFamilyName) {
                print("font: \(fontName)")
            }
        }
    

    此外,请仔细检查是否已导入字体文件并将其添加到plist中。plist上的字体名必须与上面代码中的字体名相同。