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

40,533

Solution 1

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.

Solution 2

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.

Solution 3

Swift 3

Creating a Swift extension allows you to define your own custom colors and use them just like the built in colors.

UIColor+CustomColor.swift

import UIKit

extension UIColor {

    class var customGreen: UIColor {
        let darkGreen = 0x008110
        return UIColor.rgb(fromHex: darkGreen)
    }

    class func rgb(fromHex: Int) -> UIColor {

        let red =   CGFloat((fromHex & 0xFF0000) >> 16) / 0xFF
        let green = CGFloat((fromHex & 0x00FF00) >> 8) / 0xFF
        let blue =  CGFloat(fromHex & 0x0000FF) / 0xFF
        let alpha = CGFloat(1.0)

        return UIColor(red: red, green: green, blue: blue, alpha: alpha)
    }
}

Usage:

view.backgroundColor = UIColor.customGreen

enter image description here

Solution 4

[UIColor colorWithRed:51.0 / 255.0, green:0.0, blue:153.0 / 255.0];

As long as you use a floating point value in your division you don't have to cast anything. Make sure you use floating point values. For example: 33 / 255 = 0. Which will become black.

Solution 5

There are a couple of ways to create a color.

I prefer to use the RGB method. If you use the RGB values, divide them by 255 (I do not remember why, but I know you need to do it).

float rd = 225.00/255.00;
float gr = 177.00/255.00;
float bl = 140.00/255.00;
[label setTextColor:[UIColor colorWithRed:rd green:gr blue:bl alpha:1.0]];

Hope this helps.....

Share:
40,533
Jab
Author by

Jab

Daily grind by day, code hobbiest by night, I truly enjoy helping others and myself become better programmers! Some of my favorite answers by far: How to remove all string elements from a list of integers, with strings next to integer Create UIActionSheet 'otherButtons' by passing in array, not varlist Python Pandas: Does 'loc' and 'iloc' stand for anything? Placeholder objects in Interface Builder Trying to use multiple arguments for .append, anyway how? How to make nested enum also have value

Updated on October 12, 2020

Comments

  • Jab
    Jab over 3 years

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

  • Jab
    Jab over 14 years
    But would that allow me to say something like: UIColor *orangeColor = [UIColor alloc] initWithRed:greenLblue:alpha:]; and then use it later?
  • gerry3
    gerry3 over 14 years
    Yes, with the correct syntax. I added code examples. Make sure to properly manage the memory: if you use the class convenience constructor, then you need to retain the color object or set a retained property to it.
  • gerry3
    gerry3 over 14 years
    The red, green, blue, and alpha values that are passed to the constructor need to be between 0.0 and 1.0.
  • griotspeak
    griotspeak over 12 years
    App delegates want to be good. But then they learn about things. They learn about things they have no business knowing.
  • adjwilli
    adjwilli almost 12 years
    I'm using a similar solution but I have to called something like: [[UIColor alloc] MyColor] for it to work. Is there a way to not have to allocate it?
  • Joe Masilotti
    Joe Masilotti over 11 years
    @adjwilli Make sure you are using a + in front of the method declaration, not a -. This will make it a class method vs. an instance method.
  • amallard
    amallard over 7 years
    This needs to be at the top of the answers list.