Search code examples
objective-ciosxcodeuicolor

Globally declare UIColor in project


Possible Duplicate:
Objective C defining UIColor constants

I'd like to use few colours throughout my app. Instead of creating a UIColor at every instance or in every viewController is there a way by which i can use it throughout the app.

or is it wise to use a ColourConstants.h header file where i #define each colour i want to use

i.e

#define SCARLET [UIColor colorWithRed:207.0/255.0 green:47.0/255.0 blue:40.0/255.0 alpha:1];

thanks in advance!


Solution

  • I would use a category on UIColor. For example:

    // In UIColor+ScarletColor.h
    
    @interface UIColor (ScarletColor)
    
    + (UIColor*)scarletColor;
    
    @end
    
    
    // In UIColor+ScarletColor.m
    
    @implementation UIColor (ScarletColor)
    
    + (UIColor*)scarletColor {
        return [UIColor colorWithRed:207.0/255.0 green:47.0/255.0 blue:40.0/255.0 alpha:1];
    }
    
    @end
    

    And when you want to use the color you only have to do this:

    #import "UIColor+ScarletColor.h"
    
    ....
    
    UIColor *scarlet = [UIColor scarletColor];
    

    Hope it helps!!