如何将UIColor值转换为指定的颜色string?
我需要将UIColor
转换为颜色名称的NSString
。
我试过了:
NSString *colorString = NSStringFromClass([[UIColor redColor] class]);
但colorString
没有给@“redColor”。
你想做什么? 看一下通用-desciption
方法的一个开始
UIColor* someColor = ...//Initialize color NSString* colorString = [someColor description];
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
返回指定颜色组件的格式化string。
string表示总是有四个组成部分 – 红色,绿色,蓝色和阿尔法。
https://developer.apple.com/documentation/coreimage/cicolor/1437910-stringrepresentation
colorWithString:
使用由string指定的RGBA颜色分量值创build颜色对象。
https://developer.apple.com/documentation/coreimage/cicolor/1438059-colorwithstring
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]];
完成。
如果要将string转换回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中的一个属性? 如果是这样,请查看我对这个问题的回答: 核心数据数据模型:UIColor的属性types
尝试这个 。 最好的解决scheme,我的问题。也可以帮助你们
这个怎么样:
- (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}”。
这是我用来从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]; }
这是将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)])]; }
NSString *colorString = (NSString *)YourColor;