Set RGB value for label text

I have color codes like R: 38 G: 171 B: 228, but when I set the values ​​as .38f to the color with red: green: blue: I can’t get the desired color:

[CategoryLbl setTextColor:[UIColor colorWithRed:.38f green:.171f blue:.226f alpha:1.0f]]; 

Please, help.

+8
uilabel rgb
source share
1 answer

You mix two scales: UIColour looks like it uses 0-1 floating-point values, while regular RGB values ​​are 0-255. Instead you want

  38 / 255 = 0.1491f 171 / 255 = 0.6706f 226 / 255 = 0.8863f 

So

 [CategoryLbl setTextColor:[UIColor colorWithRed:0.1491f green:0.6706f blue:0.8863f alpha:1.0f]]; 

There may be better ways to do this, for example. using values ​​0-255 - I don't know OSX / iPhone very well.

It actually looks like you can just do:

 [CategoryLbl setTextColor:[UIColor colorWithRed:(38/255.f) green:(171/255.f) blue:(226/255.f) alpha:1.0f]]; 

which is easier to understand (although I gave you enough dp, the first one should be accurate).

+22
source share

All Articles