0

I am a beginner and am currently trying to make a method for a specific custom colour. I will be using it to change the background colours of certain buttons.

I am trying to make it so that I can simply type myButton.backgroundColor = [UIColor customColor]; instead of having to type myButton.backgroundColor = [UIColor colorWithRed:0.643 green:0.643 blue:0.643 alpha:1];.

4

1 回答 1

0

You need write category class for UIColor something like

in UIColor+Custom.h:

define color enums something like

 typedef enum color {RED=0,GREEN, PURPLE, YELLOW} color; //define more enums for other custom color


@interface UIColor (Custom)

+ (UIColor *)customColor:(color)color;


@end



@implementation UIColor (Custom)

+ (UIColor *)customColor:(color)color 
{
    static UIColor color = nil;
    switch (color) {
    case RED:
        color = [UIColor reColor];
        break;
    case YELLOW:
        color = [UIColor colorWithRed:0.643 green:0.643 blue:0.643 alpha:1]; //just a example not right rgb for yellow
        break;
    //Similarly write for other color's     
    default:
        break;


return color;

}

于 2013-10-19T19:49:31.477 回答