【问题标题】:ios - Can we change Font, Size for all Label by single code / file?ios - 我们可以通过单个代码/文件更改所有标签的字体、大小吗?
【发布时间】:2019-12-19 21:32:29
【问题描述】:

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

谢谢!

【问题讨论】:

    标签: ios swift uilabel


    【解决方案1】:

    有几种方法可以实现这一目标

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

    您每次都需要使用方便的 init。

    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 的不同样式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多