Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The default text color for all UILabels

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?

like image 643
Adrian Bobrowski Avatar asked Dec 22 '16 07:12

Adrian Bobrowski


1 Answers

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

    }
like image 66
Muhammad Adnan Avatar answered Sep 30 '22 04:09

Muhammad Adnan