ios - 如何将自定义字体应用于整个框架中的所有 Labels、UITextFields 和 UIButtons?

标签 ios swift user-interface fonts ios-frameworks

我正在努力创建我自己的具有通用屏幕和功能的框架。我坚持要实现以下目标:

  • 我在框架内有 3 个屏幕,它们有 UILabelUITextField
  • 我想将字体应用到整个应用程序中使用的框架内的所有 UILabelUITextField

Example: If I am using "Kingthings_Trypewriter_2.ttf" in my application then by writing this line should change the font of all respective UILabels and UITextFields.

UILabel.appearance().font = UIFont(name: "Kingthings_Trypewriter_2", size: 20)
UITextView.appearance().font = UIFont(name: "Kingthings_Trypewriter_2", size: 20)
  • 仍然,通过使用上面的代码,它适用于应用程序的标签和 UITextFields,但不适用于 Frameworks 的标签和 UITextFields。

我们将不胜感激。

最佳答案

声明一个特定的类并为整个框架使用该自定义标签或 TextView 。

  class FrameWorkLabel: UILabel{

var frameWorkFont: UIFont?


private func setupViews(){
    font = frameWorkFont ?? UIFont.frameWorkFont
}

override init(frame: CGRect) {
    super.init(frame: frame)
    setupViews()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setupViews()
}

 }

  class FrameWorkTextField: UITextField{

var frameWorkFont: UIFont?

private func setupViews(){
    font = frameWorkFont ?? UIFont.frameWorkFont
}

override init(frame: CGRect) {
    super.init(frame: frame)
    setupViews()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setupViews()
}
 }






extension UIFont{
static let frameWorkFont = UIFont.systemFont(ofSize: 18)
}

在项目中的任何地方使用标签或文本字段的实例。

@IBOutlet weak var label : FrameWorkLabel!
//or
let label1 = FrameWorkLabel()

@IBOutlet weak var textField : FrameWorkTextField!

let textField1 = FrameWorkTextField()

关于ios - 如何将自定义字体应用于整个框架中的所有 Labels、UITextFields 和 UIButtons?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51759058/

相关文章:

python - 如何在 emacs 终端中启用 `flymake-display-err-menu-for-current-line`?

iOS "widget"风格界面

ios - 有没有办法检查 Objective-c 提供的方法的时间复杂度?

ios - 从 Xcode 构建更快的 "release"?

swift - 解析推送通知注册 - 如果用户未登录则崩溃

ios - 如何使用 Swift 创建 "hyperlink"?

iOS黑暗模式。用户界面样式在进入后台时来回更改

java - 在 Java GUI 中暂停执行

ios - 如何使 UITextView 能够接收粘贴的图像

ios - 引用核心数据实体