iPhone Developer Tips Visitor Stats: 126,691 Pageviews and 94,296 visitors in the past 30 days.
|
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.
Share with iOS Developers:
Comments
8 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];
Thanks or this. It was just what I needed!
just what the iPhone doctor ordered! thanx for this one, dude!
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
Geoff,
Did you try the macro definitions all on one line (I split the lines so they would fit on the post without scrolling)?
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.
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?
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.