Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store and get UIColor from .plist file

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?

like image 277
kopproduction Avatar asked Aug 30 '11 12:08

kopproduction


3 Answers

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 );
like image 170
jalmatari Avatar answered Nov 07 '22 06:11

jalmatari


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

like image 29
Marek Sebera Avatar answered Nov 07 '22 06:11

Marek Sebera


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].

like image 4
Geri Borbás Avatar answered Nov 07 '22 05:11

Geri Borbás