Take the 2-minute tour ×

I need to convert a UIColor to an NSString with the name of the color i.e.

[UIColor redColor];

should become


I've already tried[UIColor redColor].CIColor.stringRepresentation but it causes a compiler error

share|improve this question
See the top-rated answer here; it may assist: stackoverflow.com/questions/1748350/… –  Luke Dec 15 '12 at 20:06
You can't do what you want without writing your own method. The answers people are pointing you to are going to give you something like "1 0 0 1" or "UIDeviceRGBColorSpace 1 0 0 1". –  rdelmar Dec 15 '12 at 20:24
How do you want to get this color name -- that is, how are you going to use it? –  rdelmar Dec 15 '12 at 20:42
add comment

2 Answers

Just expanding on the answer that @Luke linked to which creates a CGColorRef to pass to CIColor:

CGColorRef colorRef = [UIColor grayColor].CGColor;  

You could instead simply pass the CGColor property of the UIColor you're working on like:

NSString *colorString = [[CIColor colorWithCGColor:[[UIColor redColor] CGColor]] stringRepresentation];

Don't forget to import the Core Image framework.

Side note, a quick and easy way to convert back to a UIColor from the string could be something like this:

NSArray *parts = [colorString componentsSeparatedByString:@" "];
UIColor *colorFromString = [UIColor colorWithRed:[parts[0] floatValue] green:[parts[1] floatValue] blue:[parts[2] floatValue] alpha:[parts[3] floatValue]];
share|improve this answer
This will not give him "GrayColor" which is what he asked for. –  rdelmar Dec 15 '12 at 20:25
@rdelmar Interesting, I guess I skipped over that detail... researching... –  0x7fffffff Dec 15 '12 at 20:26
I tried both this and the link but both of them returned the rgb+alpha equivalent. I want to know the name of the color so [uicolor whitecolor] would return white NOT 1 1 1 1 –  Alex Dec 16 '12 at 0:39
Does not work. Gave me RGBa value. –  rohan-patel Jun 26 at 18:42
add comment

This is the shortest way to convert UIColor to NSString:

- (NSString *)stringFromColor:(UIColor *)color
    const size_t totalComponents = CGColorGetNumberOfComponents(color.CGColor);
    const CGFloat * components = CGColorGetComponents(color.CGColor);
    return [NSString stringWithFormat:@"#%02X%02X%02X",
            (int)(255 * components[MIN(0,totalComponents-2)]),
            (int)(255 * components[MIN(1,totalComponents-2)])
            (int)(255 * components[MIN(2,totalComponents-2)])];
share|improve this answer
add comment

Your Answer


