Store and get UIColor from .plist file
Asked Answered
F

3

11

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?

Fireback answered 30/8, 2011 at 12:19 Comment(0)
S
7

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

Solutrean answered 30/8, 2011 at 12:29 Comment(0)
C
4

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

Cancan answered 27/8, 2013 at 16:11 Comment(0)
R
1

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 );
Robinet answered 29/7, 2018 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.