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

时间:2019-12-19 21:32:29

标签: ios swift uilabel

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

谢谢!

1 个答案:

答案 0 :(得分:0)

有几种方法可以实现

  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创建处理不同样式的自定义方式。
相关问题