【发布时间】:2013-08-28 00:16:23
【问题描述】:
是否可以以某种方式将颜色代码保存在 Plist 文件中?
我必须用字符串表示它们吗?然后我可以从它们中创建颜色吗?
我在这里看到了一个类似的帖子,但还没有答案
可以做什么?
【问题讨论】:
是否可以以某种方式将颜色代码保存在 Plist 文件中?
我必须用字符串表示它们吗?然后我可以从它们中创建颜色吗?
我在这里看到了一个类似的帖子,但还没有答案
可以做什么?
【问题讨论】:
我可以给你一个建议。您可以将 RGB 值存储在一个数组中,并将该数组存储在 plist 的每一行中,而不是存储颜色名称。
键 - 红色 类型 - 数组 值 - 1.0,0.0,0.0
检索每个键的数组。
NSArray *colorsArray = [dictionaryFromPlist objectForKey:@"Red"];
UIColor *mycolor = [UIColor colorWithRed:[[colorsArray objectAtIndex:0] floatValue]
green:[[colorsArray objectAtIndex:1] floatValue]
blue:[[colorsArray objectAtIndex:2] floatValue]
alpha:1.0];
只是我的想法..
【讨论】:
【讨论】:
你可以使用继承自NSCoder的NSKeyedArchiver
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[UIColor purpleColor]];
[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"color": data}];
要恢复颜色,您可以使用 NSKeyedUnarchiver:
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
NSLog(@"%@", [NSKeyedUnarchiver unarchiveObjectWithData: dict[@"color"]]);
【讨论】:
为了保持人类可读性,我为此做了一个分类:
@implementation UIColor (EPPZRepresenter)
NSString *NSStringFromUIColor(UIColor *color)
{
const CGFloat *components = CGColorGetComponents(color.CGColor);
return [NSString stringWithFormat:@"[%f, %f, %f, %f]",
components[0],
components[1],
components[2],
components[3]];
}
UIColor *UIColorFromNSString(NSString *string)
{
NSString *componentsString = [[string stringByReplacingOccurrencesOfString:@"[" withString:@""] stringByReplacingOccurrencesOfString:@"]" withString:@""];
NSArray *components = [componentsString componentsSeparatedByString:@", "];
return [UIColor colorWithRed:[(NSString*)components[0] floatValue]
green:[(NSString*)components[1] floatValue]
blue:[(NSString*)components[2] floatValue]
alpha:[(NSString*)components[3] floatValue]];
}
@end
与 NSStringFromCGAffineTransform 使用的格式相同。这实际上是 [eppz!kit at GitHub][1] 中更大规模 plist 对象表示器的一部分。
【讨论】: