从 .plist 文件存储和获取 UIColor

发布于 2024-12-02 03:04:44 字数 189 浏览 1 评论 0原文

我已经寻找这个有一段时间了但没有成功。我的问题是:是否有一种简单的方法来存储和获取 UIColors,例如 [UIColor blackColor][UIColor colorWithRed:0.38 green:0.757 blue:1 alpha:1]; 在我的应用程序目录中的 .plist 文件中?

I've been searching for this for a while now with no success. My question is: is there an easy way to store and get UIColors such as [UIColor blackColor] or [UIColor colorWithRed:0.38 green:0.757 blue:1 alpha:1]; in a .plist file in my app directory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蝶舞 2024-12-09 03:04:44

根据 此讨论 你有两个选项:

  1. 将其像 NSData 一样存储在 .plist 文件的数据字段中
  2. 将其像字符串 UIColor 表示一样存储

NSData 选项

NSData *theData = [NSKeyedArchiver archivedDataWithRootObject:[UIColor greenColor]];

NSString 选项

NSString *color = @"greenColor";
[UIColor performSelector:NSSelectorFromString(color)]

在此处阅读更多信息: http://www.iphonedevsdk.com/forum/iphone -sdk-development/27335-setting-uicolor-plist.html

according to this discussion you have two options:

  1. Store it like NSData in Data field of .plist file
  2. Store it like String UIColor representation

NSData option

NSData *theData = [NSKeyedArchiver archivedDataWithRootObject:[UIColor greenColor]];

NSString option

NSString *color = @"greenColor";
[UIColor performSelector:NSSelectorFromString(color)]

read more here: http://www.iphonedevsdk.com/forum/iphone-sdk-development/27335-setting-uicolor-plist.html

花期渐远 2024-12-09 03:04:44

如果你想让它保持人类可读性,
我为此创建了一个类别:

@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 使用的相同格式。这实际上是 [GitHub 上的 eppz!kit][1] 中更大规模的 plist 对象表示的一部分。

If you want to keep it human readabe,
I did a category for this:

@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

The same formatting that is used by NSStringFromCGAffineTransform. This is actually a part of a bigger scale plist object representer in [eppz!kit at GitHub][1].

萝莉病 2024-12-09 03:04:44

Objective-c 中最好且可读的方法是将其保存为十六进制字符串,例如:“#1A93A8”,然后通过 extern 方法获取它;

在 .h 文件中:

extern UIColor *colorFromHEX(NSString *hex);
extern NSString *HEXFromColor(UIColor *color);

在 .m 文件中:

UIColor *colorFromHEX(NSString *hex){
    NSString *stringColor = hex;
    int red, green, blue;
    sscanf([stringColor UTF8String], "#%02X%02X%02X", &red, &green, &blue);

    UIColor *color = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
    return color;
}


NSString *HEXFromColor(UIColor *color){
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    size_t count = CGColorGetNumberOfComponents(color.CGColor);

    if(count == 2){
        return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
                lroundf(components[0] * 255.0),
                lroundf(components[0] * 255.0),
                lroundf(components[0] * 255.0)];
    }else{
        return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
                lroundf(components[0] * 255.0),
                lroundf(components[1] * 255.0),
                lroundf(components[2] * 255.0)];
    }
}

在任何位置

NSDictionary *Config = [[NSDictionary alloc] initWithContentsOfFile:[NSBundle.mainBundle pathForResource:@"Config" ofType:@"plist"]];

UIColor *color = colorFromHEX( Config[@"color"] );
NSString *strColor = HEXFromColor( UIColor.blackColor );

The best and readable way in Objective-c is to save it as hex string like: "#1A93A8", then to get it by extern method;

in .h file:

extern UIColor *colorFromHEX(NSString *hex);
extern NSString *HEXFromColor(UIColor *color);

in .m file:

UIColor *colorFromHEX(NSString *hex){
    NSString *stringColor = hex;
    int red, green, blue;
    sscanf([stringColor UTF8String], "#%02X%02X%02X", &red, &green, &blue);

    UIColor *color = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
    return color;
}


NSString *HEXFromColor(UIColor *color){
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    size_t count = CGColorGetNumberOfComponents(color.CGColor);

    if(count == 2){
        return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
                lroundf(components[0] * 255.0),
                lroundf(components[0] * 255.0),
                lroundf(components[0] * 255.0)];
    }else{
        return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
                lroundf(components[0] * 255.0),
                lroundf(components[1] * 255.0),
                lroundf(components[2] * 255.0)];
    }
}

in any where

NSDictionary *Config = [[NSDictionary alloc] initWithContentsOfFile:[NSBundle.mainBundle pathForResource:@"Config" ofType:@"plist"]];

UIColor *color = colorFromHEX( Config[@"color"] );
NSString *strColor = HEXFromColor( UIColor.blackColor );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文