我是初学者,目前正在尝试为特定的自定义颜色制作方法。我将用它来改变某些按钮的背景颜色。
我正在尝试制作它,以便我只需输入myButton.backgroundColor = [UIColor customColor];
,而不必输入myButton.backgroundColor = [UIColor colorWithRed:0.643 green:0.643 blue:0.643 alpha:1];
。
答案 0 :(得分:0)
您需要为UIColor编写类别类
在UIColor + Custom.h中:
定义类似
的颜色枚举 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;
}