6

I have hex value in mind that I need to implement, working on ipad by the way. Anyways, how can I go about implementing that with objective-c

Help much needed, pleas and thank you!

Shane Da Silva
  • 268
  • 6
  • 16

1 Answers1

22

UIColor's range is from 0 to 1. So you just need to convert the hex color string into decimal, then divide by 255 to get the desired numbers.

For example, if the color is #E0EAF1:

  1. Convert hex to decimal: E0 → 224, EA → 234, F1 → 241
  2. Divide by 255: 224 → 0.878, 234 → 0.918, 241 → 0.945

So to create this color, use

UIColor* clr = [UIColor colorWithRed:0.878f green:0.918f blue:0.945f alpha:1];

or let the compiler do the calculation for you:

UIColor* clr = [UIColor colorWithRed:0xE0/255.0f
                               green:0xEA/255.0f
                                blue:0xF1/255.0f alpha:1];
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005