How do I make my own custom UIColor's other than the preset ones?

Jab picture Jab · Dec 24, 2009 · Viewed 40k times · Source

I want to make my own RGB colors that are UIColors and that I could use just like UIColor blackColor or any other.

Answer

EEE picture EEE · Dec 25, 2009

You can write your own method for UIColor class using categories.

#import <UIKit/UIKit.h>
@interface UIColor(NewColor)
+(UIColor *)MyColor;
@end

#import "UIColor-NewColor.h"
@implementation UIColor(NewColor)
+(UIColor *)MyColor {
     return [UIColor colorWithRed:0.0-1.0 green:0.0-1.0 blue:0.0-1.0 alpha:1.0f];
}

By this way, you create a new color and now you can call it like

[UIColor MyColor];

You can also implement this method to obtain random color. Hope this helps.