Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External Constants for UIColor, UIFont, etc

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?

like image 585
Jim Avatar asked Aug 19 '11 22:08

Jim


2 Answers

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.

like image 164
superjessi Avatar answered Oct 30 '22 07:10

superjessi


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.

like image 27
lbrndnr Avatar answered Oct 30 '22 08:10

lbrndnr