Search code examples
iosswiftuiappearance

Swift - UIAppearance Class


I have label in my Storyboard connected to my View Controller via an IBOutlet

I have extended UILabel as below

extension UILabel {
    func winnerLabel() -> UILabel {
        let winnerLabel = UILabel()
        winnerLabel.textColor = UIColor.red
        winnerLabel.textAlignment = .left

        return winnerLabel
    }
}

In my view controller I would like to set the label from the storyboard to have the same appearance as the winnerLabel.

What's the best way to set the style properties of the label I have an outlet for to the the winnerLabel?

I can do them individually, ie textColor = winnerLabel.text color, but think there must be a better way.


Solution

  • Don’t use a function. Use a UILabel subclass that initializes itself with the desired features, and make the label in the storyboard an instance of that subclass.