Please consider subscribing to Mobile Developer Tips RSS feed or following us on Twitter
|
|
|
Below are two macros I paste inside every new iPhone project. Besides saving a few keystrokes, they work well when using the color picker application. Let’s look at the macros first:
#define RGB(r, g, b)
[UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
#define RGBA(r, g, b, a)
[UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
Here’s the code without/with the macro:
// Code without the macro
msgLabel.textColor =
[UIColor colorWithRed:255/255.0 green:251/255.0 blue:204/255.0 alpha:1];
// Or like this...
msgLabel.textColor = [UIColor colorWithRed:1.0 green:.98 blue:.8 alpha:1];
// Code with macro
msgLabel.textColor = RGB(255, 251, 204);
UIColor expects its parameters to be expressed as values in the range of 0.0 to 1.0, so I pass in the color value I’m looking for between 0 and 255, and let the macro do the math for me.
Let me show you how I use the macro with the color picker application. The figure below shows a screenshot of the color picker – at this point I have selected the color I am interested in, so I choose rgb from the dropdown menu.

From here, I copy the value from the field “Hex:”, paste this into my project and finish by changing rgb to RGB.
Related posts:
- Using NSScanner to convert Hex to RGB Color
- Debugging Macros
- Xcode Code Completion Macros
Comments
11 Responses to “UIColor Macros”
Leave Comment
Interesting approach, using a macro. Instead of this, I added a category to NSString to add a “hexColor” method, to convert a string to a UIColor object.
http://bit.ly/NSString-HexColor
That allows you to say something like:
NSString *colorStr = @”#ff03ca”;
UIColor *color = [colorStr colorFromHex];
[Reply]
Thanks or this. It was just what I needed!
[Reply]
just what the iPhone doctor ordered! thanx for this one, dude!
[Reply]
Where do you paste the macro in at? I tried after my import statements in my app delegate and I get compiler errors:
error: expected identifier or ‘(‘ before ‘[‘ token
[Reply]
John Muchow Reply:
July 5th, 2010 at 4:31 pm
Geoff,
Did you try the macro definitions all on one line (I split the lines so they would fit on the post without scrolling)?
[Reply]
Thanks that fixed the compiler problem. However next it tells me that I’m implicitly defining that function when I go to use it. I assumed that placing it in a random .h file anywhere would make it globally available. When that turned out incorrect I tried including the .h file but that didn’t work either. I guess I just need to research how to use #define statements in terms of where to place them and how to reuse across multiple files.
[Reply]
John Muchow Reply:
July 11th, 2010 at 9:05 pm
Geoff,
You should be able to copy the definitions into a .h file that you include wherever needed. Something else may be amiss, as I just copy/pasted the defines into a source file with no errors. If you remove the definitions does your app compile as expected?
[Reply]
I finally got it, thanks though. I had originally placed inside @interface and @end and then had deleted everything in the .h minus the #define. I added a new file with #define inside @protocol and @end and included that .h wherever needed.
The next problem I ran into was that the color change was not visible at runtime. I had to move my code to set the cell background color from cellForRowAtIndexPath to willDisplayCell, then it showed up as expected.
Thanks again, and nice macro.
[Reply]
very nice and clean…
I use this one I stumbled upon months ago…
https://gist.github.com/631616
[Reply]
Thanks so much for this, it’s proven to be a huge time saver.
Genius!
[Reply]
or:
#define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
#define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#define HTML(rgb) RGB((double)(rgb >> 16 & 0xff), (double)(rgb >> 8 & 0xff), (double)(rgb & 0xff))
then:
[label setTextColor:HTML(0xff0000)];
[Reply]