sen*_*thu 16 iphone cocoa-touch ios
我需要转换UIColor
为NSString
颜色名称.
我试过了:
NSString *colorString = NSStringFromClass([[UIColor redColor] class]);
Run Code Online (Sandbox Code Playgroud)
但colorString
没有给@"redColor".
Zel*_*lko 32
在CIColor
类中包含的颜色值和色空间,其颜色值都有效.
https://developer.apple.com/documentation/coreimage/cicolor
// UIColor to NSString
CGColorRef colorRef = [UIColor grayColor].CGColor;
NSString *colorString = [CIColor colorWithCGColor:colorRef].stringRepresentation;
Run Code Online (Sandbox Code Playgroud)
// NSString to UIColor
CIColor *coreColor = [CIColor colorWithString:@"0.5 0.5 0.5 1.0"];
UIColor *color = [UIColor colorWithCIColor:coreColor];
Run Code Online (Sandbox Code Playgroud)
- - - - - - - - -警告 - - - - - -
如果你想支持所有设备上面提到的将NSString转换为UIColor的方法将不适用于所有设备.
stringRepresentation
返回指定颜色组件的格式化字符串.
字符串表示始终具有四个组件 - 红色,绿色,蓝色和alpha.
https://developer.apple.com/documentation/coreimage/cicolor/1437910-stringrepresentation
colorWithString:
使用由字符串指定的RGBA颜色分量值创建颜色对象.
https://developer.apple.com/documentation/coreimage/cicolor/1438059-colorwithstring
Ros*_*one 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]];
Run Code Online (Sandbox Code Playgroud)
完成.
如果要将字符串转换回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];
Run Code Online (Sandbox Code Playgroud)
您是否将UIColor对象存储为Core Data中的属性?如果是这样,请查看我对此问题的回答:核心数据数据模型:UIColor的属性类型
Vla*_*mir 10
你想做什么的?看一下-desciption
开始的通用方法
UIColor* someColor = ...//Initialize color
NSString* colorString = [someColor description];
Run Code Online (Sandbox Code Playgroud)
试试这个 .我的问题的最佳解决方案.也可以帮助你们..
https://github.com/daniel-beard/DBColorNames
归档时间: |
|
查看次数: |
27080 次 |
最近记录: |