UIAppearance not taking effect on UILabels created programmatically

前端 未结 5 1944
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-07 07:34

We have extended UILabel to be able to apply standard fonts and colors for all uses of a given label type in our apps. Eg.

@interface UILabelHeadingBold : UILab         


        
5条回答
  •  不思量自难忘°
    2021-02-07 08:25

    A workaround that I've used is to manually apply the color from the appearance that is set:

    let label = UILabel()
    label.textColor = UILabel.appearance().textColor
    

    This way you don't need to reference anything new, or explicitly define the color. This also works for context specific coloring:

    label.textColor = UILabel.appearance(whenContainedInInstancesOf:[MyView.self]).textColor
    

提交回复
热议问题