Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 299
Jab Avatar asked Dec 24 '09 03:12

Jab


People also ask

What is CGColor?

CGColor is the fundamental data type used internally by Core Graphics to represent colors. CGColor objects, and the functions that operate on them, provide a fast and convenient way of managing and setting colors directly, especially colors that are reused (such as black for text).


2 Answers

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.

like image 95
EEE Avatar answered Oct 17 '22 13:10

EEE


I needed to define a couple of custom colors for use in several places in an app - but the colours are specific to that app. I thought about using categories, but didn't want to have extra files to include every time. I've therefore created a couple of static methods in my App delegate.

In MyAppDelegate.h

+ (UIColor*)myColor1;

In MyAppDelegate.m

+ (UIColor*)myColor1 {  
return [UIColor colorWithRed:26.0f/255.0f green:131.0f/255.0f blue:32.0f/255.0f alpha:1.0f];  
}

I have a method per color, or you could do a single method and add a parameter.

I can then use it anywhere in the app like this:

myView.backgroundColor = [MyAppDelegate myColor1];

I hope this helps someone else.

like image 27
James Avatar answered Oct 17 '22 12:10

James