9

Regarding saving the UIColor in a Plist: I have tried different ways but not been able to do so, I want to save and retrieve the color values in a plist file.

I can not extract the data value of the color using nslog and save it in the plist.

Is there any other way to do so?

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
N.K
  • 2,220
  • 1
  • 14
  • 44

4 Answers4

7

I prefer using string to store the color. The parsing code that does this shown below (cut out from https://github.com/xslim/TKThemeManager/blob/master/TKThemeManager.m#L162)

+ (UIColor *)colorFromString:(NSString *)hexString {    
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    unsigned hex;
    BOOL success = [scanner scanHexInt:&hex];

    if (!success) return nil;
    if ([hexString length] <= 6) {
        return UIColorFromRGB(hex);
    } else {
        unsigned color = (hex & 0xFFFFFF00) >> 8;
        CGFloat alpha = 1.0 * (hex & 0xFF) / 255.0;
        return UIColorFromRGBA(color, alpha);
    }
}
Taras Kalapun
  • 1,761
  • 15
  • 19
3

For a quick solution (but maybe not the most pretty one):

  • Add the color property as a type Number into the plist
  • Enter the color as an RGB-hexdecimal, for example: 0xff00e3
  • Read it out and process it with a macro like below

Here is a code example:

// Add this code to some include, for reuse
#define UIColorFromRGBA(rgbValue, alphaValue) ([UIColor colorWithRed:((CGFloat)((rgbValue & 0xFF0000) >> 16)) / 255.0 \
                                                               green:((CGFloat)((rgbValue & 0xFF00) >> 8)) / 255.0 \
                                                                blue:((CGFloat)(rgbValue & 0xFF)) / 255.0 \
                                                               alpha:alphaValue])

// This goes into your controller / view
NSDictionary *myPropertiesDict = [NSDictionary dictionaryWithContentsOfFile:...];
UIColor *titleColor = UIColorFromRGBA([myPropertiesDict[@"titleColor"] integerValue], 1.0);

After entering the color as hexdecimal, the plist editor will show it as a decimal number. Not nice. As a developer you normally copy paste the colors from a design document anyway, so the need to read the color values is not that big.

Berik
  • 7,816
  • 2
  • 32
  • 40
1

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

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
  • Just to note that the red, green, blue values are 0.0-1.0 not 0-255 so divide them by 255 to get correct value - this caught me out for a while. – amergin Feb 07 '14 at 13:50
  • This is for storing in `plist`, you probably want to "design" the colors in `plist`. For RGB conversion helpers, see http://stackoverflow.com/questions/13224206/how-do-i-create-an-rgb-color-with-uicolor/21297254#21297254 and http://stackoverflow.com/questions/437113/how-to-get-rgb-values-from-uicolor/21296829#21296829. – Geri Borbás Feb 07 '14 at 15:42
-1

Best solution I found for this problem is on the following site:

http://arstechnica.com/apple/guides/2009/02/iphone-development-accessing-uicolor-components.ars

After getting the string from a UIColor, it should be an easy enough task to save it to a plist file and retrieve it later on.

gchbib
  • 1,955
  • 1
  • 17
  • 23