Search code examples
iosuikituicolor

Why does UIColor.red work but using the initializer of the UIColor does not?


I am setting a label colour in a view controller class. When I use the following code, it works.

self.labelTest.textColor = UIColor.red

enter image description here

However, if I use the initializer of UIColor, like the following, the label becomes "invisible".

self.labelTest.textColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0)

n

Can someone tell me why this is so?


Solution

  • It is because you have set alpha to 0

    self.labelTest.textColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0)

    set alpha to 1 in order to make it visible

    self.labelTest.textColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1)
    

    From documentation:

    alpha

    The opacity value of the color object, specified as a value from 0.0 to 1.0. Alpha values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.