The default text color for all UILabels

后端 未结 2 389
醉梦人生
醉梦人生 2021-02-05 18:07

I want to set the default text color for all UILabels in my app. I found this:

UILabel.appearance().textColor = UIColor.red

This works, but whe

相关标签:
2条回答
  • 2021-02-05 18:32

    There is also an alternative to what Muhammad suggested. The alternative is to use the UIAppearance proxy, but specify the classes where it would be applied to.

    In that case, you would use it like this:

    UILabel.appearanceWhenContainedInInstancesOfClasses([MyViewController.self, MyotherViewController.self]).textColor = UIColor.red
    

    The above though, would only allow you to set the textColor your self in some classes. in those where the proxy is set to be enabled, you won't be able to set the text color.

    It's up to you to choose which approach is better for your case

    0 讨论(0)
  • 2021-02-05 18:43

    SubClass you labels with CustomLabel.swift. You can set text color using IBDesignable property named as txtColor

    Below is the code working example

    import UIKit
    
        @IBDesignable  class CustomLabel: UILabel {
    
            @IBInspectable var txtColor: UIColor = UIColor.grayColor() {
                didSet {
                    self.textColor = txtColor
                }
            }
    
            func setup() {
               self.textColor = txtColor
            }
    
            override func awakeFromNib() {
                setup()
            }
    
            override func prepareForInterfaceBuilder() {
                setup()
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题