ios 如何将 UIColor 值转换为命名的颜色字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1748350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert UIColor value to a named color string?
提问by senthilMuthu
I need to convert UIColor
to an NSString
of the color name.
我需要转换UIColor
为NSString
颜色名称的一个。
I tried:
我试过:
NSString *colorString = NSStringFromClass([[UIColor redColor] class]);
But colorString
did not give @"redColor".
但是colorString
没有给@"redColor"。
采纳答案by Vladimir
What do you want to do that for? Have a look at generic -desciption
method for a start
你想做什么?首先看一下通用-desciption
方法
UIColor* someColor = ...//Initialize color
NSString* colorString = [someColor description];
回答by Zelko
The CIColor
class contains color values and the color space for which the color values are valid.
在CIColor
类中包含的颜色值和色空间,其颜色值都有效。
https://developer.apple.com/documentation/coreimage/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];
-----------------Warning-----------
- - - - - - - - -警告 - - - - - -
If You Want Support All Devices The Way Mentioned Above to Convert NSString to UIColor Will Not Work On All Devices.
如果你想支持所有设备,上面提到的将 NSString 转换为 UIColor 的方法不适用于所有设备。
stringRepresentation
字符串表示
Returns a formatted string that specifies the components of the color.
返回指定颜色分量的格式化字符串。
The string representation always has four components—red, green, blue, and alpha.
字符串表示总是有四个分量——红色、绿色、蓝色和 alpha。
https://developer.apple.com/documentation/coreimage/cicolor/1437910-stringrepresentation
https://developer.apple.com/documentation/coreimage/cicolor/1437910-stringrepresentation
colorWithString:
颜色带字符串:
Creates a color object using the RGBA color component values specified by a string.
使用字符串指定的 RGBA 颜色分量值创建颜色对象。
https://developer.apple.com/documentation/coreimage/cicolor/1438059-colorwithstring
https://developer.apple.com/documentation/coreimage/cicolor/1438059-colorwithstring
回答by Rose Perrone
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]];
Done.
完毕。
If you want to convert the string back to a UIColor object:
如果要将字符串转换回 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];
Are you storing a UIColor object as an attribute in Core Data? If so, check out my answer to this question: Core Data data model: attribute type for UIColor
您是否将 UIColor 对象存储为 Core Data 中的属性?如果是这样,请查看我对这个问题的回答:Core Data data model: attribute type for UIColor
回答by Arpan Dixit
Try this . Best solution for my problem.Might help you guys also..
尝试这个 。我的问题的最佳解决方案。也可以帮助你们..
回答by Kristopher Johnson
How about this:
这个怎么样:
- (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;
}
For example, stringForColor:[UIColor greenColor]
has the result "{0.000000,1.000000,0.000000,1.000000}".
例如,stringForColor:[UIColor greenColor]
结果为“{0.000000,1.000000,0.000000,1.000000}”。
回答by MoDJ
Here is what I used to convert from CGColor to NSString #RRGGBBAA format:
这是我用来从 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];
}
回答by k06a
This is the shortest way to convert UIColor to NSString:
这是将 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)])];
}