ios - 我们可以通过单个代码/文件更改所有标签的字体、大小吗?

ios - Can we change Font, Size for all Label by single code / file?

请问,有什么方法可以通过一个代码更改所有标签的字体,大小? 就像应用程序中的动态类型一样。 例如:Android 已支持 XML 文件(尺寸)

谢谢!

有几种方法可以实现

  1. 您可以像这样为所有 UILabel 提供相同的样式:
extension UILabel {
    @nonobjc
    convenience init() {
        self.init(frame: .zero)
        self.font = UIFont.systemFont(ofSize: 12, weight: UIFontWeightThin)
        //... here goes other changes
    }
}

您每次都需要使用便利的初始化程序。

  1. 另一种选择是创建一些自定义 UILabel:
class CustomLabel: UILabel {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.font = UIFont.systemFont(ofSize: 12, weight: UIFontWeightThin)
        //... here goes other changes
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        self.font = UIFont.systemFont(ofSize: 12, weight: UIFontWeightThin)
        //... here goes other changes
    }
}
  1. 最后一个选项相对更复杂,需要您创建自定义方式来处理 UILabel 的不同样式。