How to set textColor of UILabel in Swift

kag359six picture kag359six · Sep 14, 2014 · Viewed 152.5k times · Source

When I try setting the color of a UILabel to the color of another UILabel using the code

myLabel.textColor = otherLabel.textColor

It doesn't change the color. When I use this code, however,

myLabel.textColor = UIColor.redColor()

It changes the color correctly. What's the issue with the first line?

Answer

Piet Grootnoten picture Piet Grootnoten · Oct 22, 2014

The easiest workaround is create dummy labels in IB, give them the text the color you like and set to hidden. You can then reference this color in your code to set your label to the desired color.

yourLabel.textColor = hiddenLabel.textColor

The only way I could change the text color programmatically was by using the standard colors, UIColor.white, UIColor.green...