The default text color for all UILabels

advertisements

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 when I want to set some other color for a particular label in its storyboard, it cannot be done - all labels are still red.

Is it possible to define a default text color for all UILabels by setting it in storyboards and then change it for some UILabels ALSO in storyboards (not in code)? Or set the default color in code using the Appearance API and change it for some labels in storyboard?


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()
        }

    }