I'm not sure what these codes are called, I think hex codes. The code for it is 006633 and it's a dark green color. I want it to match my view backgrounds.
Asked
Active
Viewed 812 times
-5
-
`+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha`? What have you tried? – Larme May 21 '14 at 17:34
-
1Instead of asking why not Google and learn about representing colors on computer code. – zaph May 21 '14 at 17:34
-
you can use this : http://www.javascripter.net/faq/hextorgb.htm. easily googled btw – giorashc May 21 '14 at 17:34
-
I already know that, but how do I convert that code to the UIColor? – iOSAaronDavid May 21 '14 at 17:38
4 Answers
2
For your problem:
your Hex code #006633 stands for
Red 00 Green 66 Blue 33
Since they are in Hex, converting to int.
R 0 ; G 102; B 51
the iOS api set it as
[self.view setBackgroundColor:[UIColor colorWithRed:0 green:102/255.0 blue:51/255.0 alpha:1.0]];
Refere to the below links if you want to understand further
1
For ease of use when working with alternative color models, here are a couple of tools that make life easier:
- ColorSnapper: allows you to select a color using a dropper from your screen, converts to a myriad of color formats, including UIColor RGB.
- ColorUtils: an iOS Objective-C category that lets you parse HEX color codes using syntax like
[UIColor colorWithString:@"006633"]
. It's also available as a CocoaPod. I find this library particularly useful when working with REST APIs because it allows you to define a color in a single string.

brandonscript
- 68,675
- 32
- 163
- 220
-
I might try out CocoaPod later, but the only code available that I can use right now in Xcode is: [UIColor colorWithCGColor:<#(CGColorRef)#>];[UIColor colorWithCIColor:<#(CIColor *)#>];[UIColor colorWithPatternImage:<#(UIImage *)#>]; [UIColor colorwithHue:] There's also the blueColor and so on. But my Xcode will NOT take code like [UIColor colorWithString:@"006633"]; . Maybe I don't have something imported? colorWithHex does not work either. – iOSAaronDavid May 21 '14 at 19:01
-
To use that custom category you'd need to include the colorutils source with your project. You can either install and include it manually, or use the cocoapod implementation. Either way, you won't be able to use colorWithString without using that category. Also make sure you import the colorutils header (
I believe?) – brandonscript May 21 '14 at 19:03 -
ColorSnapper was the only tool i've been able to get #1A1A1A converted to `UIColor(red:0.137, green:0.137, blue:0.137, alpha:1)` perfect. THANK YOU – Cmag Oct 16 '16 at 02:50
0
+ (UIColor *)colorWithHexString:(NSString *)str {
const char *cStr = [str cStringUsingEncoding:NSASCIIStringEncoding];
long x = strtol(cStr+1, NULL, 16);
return [UIColor colorWithHex:x];
}
use this function to pass your 006633 as a NSString and get the UIColor to use on setting views.

John Riselvato
- 12,854
- 5
- 62
- 89
0
You can use the [UIColor colorwithRed: green : blue: alpha: ] class method.
UIColor *myGreenColor = [UIColor colorWithRed:0.0 green:.4 blue:.2 alpha: 1.0];
This will create your specific color composed with CGFloat values of the color percentages and a full alpha level. You can find these percentages on any color hex value website, or alternatively pass a string value of the hex into a method.

jacksonbryce
- 1
- 1
- 2