UIColor macro with hex values
Cocoa has several colors built into the UIColor class. For example:
This is great, but what if you want one of the thousands of colors not found in the pre-defined list? There's an easy way to do that:
Perfect. But what if you just have a list of hex color codes? Wouldn't it be nice if you didn't have to convert each of the color components in the hex code to its corresponding RGB decimal value? Here's where a simple and very useful macro comes into play. Just place the code below in a header file and you're done. (They're identical except that UIColorFromRGB always sets the alpha value to 1.0, whereas UIColorFromRGBWithAlpha allows you to set the alpha value.)
Read more »
[UIColor redColor]; [UIColor darkGrayColor];
This is great, but what if you want one of the thousands of colors not found in the pre-defined list? There's an easy way to do that:
[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
Perfect. But what if you just have a list of hex color codes? Wouldn't it be nice if you didn't have to convert each of the color components in the hex code to its corresponding RGB decimal value? Here's where a simple and very useful macro comes into play. Just place the code below in a header file and you're done. (They're identical except that UIColorFromRGB always sets the alpha value to 1.0, whereas UIColorFromRGBWithAlpha allows you to set the alpha value.)
Read more »