I have a constants.m file that is a centralized collection of many program constants. To set a color, I do this:
@implementation UIColor (UIColor_Constants)
+(UIColor *) defaultResultTableBackgroundColor{
//return [[UIColor colorWithRed:0.6f green:0.004f blue:0.0f alpha:1.0f] retain];
return [[UIColor colorWithRed:0.1f green:0.004f blue:0.3f alpha:0.3f] retain];
}
+(UIColor *) defaultResultHeaderBackgroundColor{
return [[UIColor clearColor] retain];
}
@end
and in the constants.h I have
@interface UIColor (UIColor_Constants)
+(UIColor *) defaultResultTableBackgroundColor;
+(UIColor *) defaultResultHeaderBackgroundColor;
@end
and then just use [UIColor defaultResultTableBackgroundColor]
where I want to refer to this constant.
I would like to have some other UIColor and UIFont constants, and, while this works, it seems to be more complicated than it needs to be. Is there an easier way to do this?
I use a constants file for the same purpose. Rather than setting up a whole interface and implementation file, I create the constants file as just the header (.h) file. Then, I define the colors I want to use, such as:
#define globalColor [UIColor colorWithRed:0.1f green:0.004f blue:0.3f alpha:0.3f]
Then, any time you use globalColor
it's just like typing in the defined code.
I actually like this way too. One question: why do you retain the uicolor? This is very dangerous. It's very likely to make a mistake and to create a memory leak.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With