2009-11-17 87 views
14

我需要将UIColor转换为颜色名称的NSString如何将UIColor值转换为指定的颜色字符串?

我想:

NSString *colorString = NSStringFromClass([[UIColor redColor] class]); 

colorString没有给@ “redColor”。

+0

你想要什么格式所产生的NSString有哪些?你打算怎么处理这个字符串? (显示给用户,保存到文件,...) – 2009-11-17 12:17:45

+1

嗨, 例如... whiteColor .. 我想将其转换为字符串数据类型..... @“whiteColor”...? – 2009-11-18 03:40:24

+0

我想转换成UTF8String ..?任何帮助..? – 2009-11-18 04:56:31

回答

10

你想为此做什么?看看通用-desciption方法一开始

UIColor* someColor = ...//Initialize color 
NSString* colorString = [someColor description]; 
+0

该解决方案在XCode调试器(XCode 4.6.3起)中效果很好。 – 2013-09-12 03:33:18

+1

不起作用,给RGBa反而'我将要使用的颜色是 - > UIDeviceRGBColorSpace 1 0 0 1' – 2014-06-26 18:38:22

+0

你得到一个调试字符串。不是颜色的字符串值 – 2017-05-23 12:00:41

4

如何:

- (NSString *)stringForColor:(UIColor *)color { 
    CGColorRef c = color.CGColor; 
    const CGFloat *components = CGColorGetComponents(c); 
    size_t numberOfComponents = CGColorGetNumberOfComponents(c); 
    NSMutableString *s = [[[NSMutableString alloc] init] autorelease]; 
    [s appendString:@"{"]; 
    for (size_t i = 0; i < numberOfComponents; ++i) { 
     if (i > 0) { 
      [s appendString:@","]; 
     } 
     [s appendString:[NSString stringWithFormat:@"%f", components[i]]]; 
    } 
    [s appendString:@"}"]; 
    return s; 
} 

例如,stringForColor:[UIColor greenColor]有结果 “{} 0.000000,1.000000,0.000000,1.000000”。

+0

实际上,-description返回相同的颜色空间信息 – Vladimir 2009-11-17 12:26:44

+0

是的,但是-description还包括类名称,它可能是或者可能不是理想的(提问者没有说出他想要的字符串看起来像)。 – 2009-11-17 12:35:44

17
UIColor *color = value; 
const CGFloat *components = CGColorGetComponents(color.CGColor); 
NSString *colorAsString = [NSString stringWithFormat:@"%f,%f,%f,%f", components[0], components[1], components[2], components[3]]; 

完成。

如果你想将字符串转换回的UIColor对象:

NSArray *components = [colorAsString componentsSeparatedByString:@","]; 
CGFloat r = [[components objectAtIndex:0] floatValue]; 
CGFloat g = [[components objectAtIndex:1] floatValue]; 
CGFloat b = [[components objectAtIndex:2] floatValue]; 
CGFloat a = [[components objectAtIndex:3] floatValue]; 
UIColor *color = [UIColor colorWithRed:r green:g blue:b alpha:a]; 

你存储的UIColor对象作为核心数据的属性?如果是的话,我的回答看看这个问题:Core Data data model: attribute type for UIColor

+0

关于如何将此字符串转换回uicolor的任何信息? :) – 2010-07-04 12:36:43

+0

嗨,不'与'whiteColor'工作。 – Maulik 2013-10-04 10:52:31

32

CIColor类中包含的颜色值和色空间,其颜色值都有效。

https://developer.apple.com/documentation/coreimage/cicolor

// UIColor to NSString 
CGColorRef colorRef = [UIColor grayColor].CGColor; 
NSString *colorString = [CIColor colorWithCGColor:colorRef].stringRepresentation; 

// NSString to UIColor 
CIColor *coreColor = [CIColor colorWithString:@"0.5 0.5 0.5 1.0"]; 
UIColor *color = [UIColor colorWithCIColor:coreColor]; 

-----------------警告-----------

如果您希望支持所有设备上面提到的将NSString转换为UIColor的方式不适用于所有设备。


stringRepresentation

返回一个格式化字符串指定颜色的成分。

字符串表示总是有四个组件 - 红色,绿色,蓝色和alpha。

https://developer.apple.com/documentation/coreimage/cicolor/1437910-stringrepresentation


colorWithString:

创建使用由字符串指定的RGBA颜色成分值的颜色对象。

https://developer.apple.com/documentation/coreimage/cicolor/1438059-colorwithstring

+2

不知道为什么这个答案有这么多upvotes。这是不正确的。 'stringRepresentation'返回一个带有颜色组件的格式化字符串,而不是OP要求的颜色名称。 – memmons 2014-04-05 17:51:33

+0

他举了两个例子,'colorString没有给@“redColor”'和'例如... whiteColor'。 – memmons 2014-04-06 16:52:02

+1

谢谢!正是我在找什么。 – Shantanu 2014-06-28 11:53:51

-9
NSString *colorString = (NSString *)YourColor; 
0

这是我用来从CGColor转换为NSString的#RRGGBBAA格式:

// Return the parsed core graphics color as a "#RRGGBBAA" string value 

+ (NSString*) cgColorToString:(CGColorRef)cgColorRef 
{ 
    const CGFloat *components = CGColorGetComponents(cgColorRef); 
    int red = (int)(components[0] * 255); 
    int green = (int)(components[1] * 255); 
    int blue = (int)(components[2] * 255); 
    int alpha = (int)(components[3] * 255); 
    return [NSString stringWithFormat:@"#%0.2X%0.2X%0.2X%0.2X", red, green, blue, alpha]; 
} 
+2

要小心,因为它假定颜色是在RGB色彩空间中,但不一定是这种情况。例如,UIColor.Black.CGColor在灰色的颜色空间中,只有两个组件(强度和alpha)。 – 2012-09-14 20:04:09

0

这是的UIColor转换成的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)])]; 
} 
+0

倒数第二行缺少一个逗号。 – Womble 2016-08-17 02:29:25

+0

@Womble谢谢! – k06a 2016-08-17 10:52:38

5

试试这个。我problem.Might帮助你们也是最好的解决方案..

https://github.com/daniel-beard/DBColorNames

+3

这是一个令人难以置信的发现,如果颜色的名称是OP要求的,也许这应该是被接受的答案。 – 2016-07-31 02:51:11

+1

这绝对应该是被接受的答案。如果你想要一个面向用户的颜色描述,这是要走的路。 – 2016-08-28 03:15:49

+0

这是最有帮助的解决方案。 – 2018-02-25 15:42:04

相关问题