IOS SDK Interface Builder RGB slider creating a different color than UIColor with RGB

In the interface builder, I changed the UILabel color to this screenshot, having Red 255, Green 159, Blue 0 and Opacity 100%. which gives an orange color.

IB RGB SlidersColor produced in center

I programmatically change the color of UILabel, than I change it to the original color, using this ...

timeLabel.textColor = [UIColor colorWithRed:255.0 green:159.0 blue:0.0 alpha:1.0]; 

and he gives this color .... Uicolor color

I thought everything should be the same, does anyone know what I'm doing wrong? please help, thanks.

+6
source share
4 answers
 timeLabel.textColor = [UIColor colorWithRed:255/255.0f green:159.0/255.0f blue:0.0/255.0f alpha:1.0]; 
+2
source

EDIT: Two years later, this post still gets some karma and comments. Fixed my old answer to the best.

The most sensible and reusable way to add a function that can take values ​​from 0 to 255 for UIColor is to create a custom category. It’s easier to read, easier to debug, easier for other people to contribute, and makes the project clean and structured, as it grows not only as view viewcontrollers. So, add the following files and import them into your m files if you need them.

UIColor + Extra.h

 @interface UIColor (Extra) + (UIColor *)colorWithR:(uint)red G:(uint)green B:(uint)blue A:(uint) alpha + (UIColor *) randomColor; + (UIColor *) colorWithHex:(uint) hex; @end 

UIColor + Extra.m

 #import "UIColor+Extra.h" @implementation UIColor (Extra) + (UIColor *)colorWithR:(uint)red G:(uint)green B:(uint)blue A:(uint) alpha { return [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/100.f]; } + (UIColor *) randomColor { CGFloat red = (CGFloat)random()/(CGFloat)RAND_MAX; CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX; CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX; return [UIColor colorWithRed:red green:green blue:blue alpha:1.0]; } + (UIColor *) colorWithHex:(uint) hex { int red, green, blue, alpha; blue = hex & 0x000000FF; green = ((hex & 0x0000FF00) >> 8); red = ((hex & 0x00FF0000) >> 16); alpha = ((hex & 0xFF000000) >> 24); return [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.f]; } @end 
+7
source

A very easy way to reproduce colors in the Xcode pencil palette is to use this link https://github.com/rob-brown/RBCategories/blob/master/UIColor+RBExtras.m

it allows colored pencils like this .... UIColor *ThisColor = [UIColor blueberryCrayonColor];

0
source

Try installing "Device RGB" - it worked for me

enter image description here

0
source

Source: https://habr.com/ru/post/908776/


All Articles