gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:57:01 24 4
gpt4 key购买 nike

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

  • 我在框架内有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com