storing uicolor in plist - iphone

I am trying to store UIColor in plist.I seen many links but none of those not given accurate answer.some of my sample code is below
CGColorRef color=[MyColor CGColor];
NSUInteger numComponents = CGColorGetNumberOfComponents(color);
const CGFloat *components=CGColorGetComponents(color);
NSArray *colorArray=[NSArray arrayWithObjects:(float)components[0],(float)components[1],nil];
it is giving Not working for me can any one tell me the exact answer to store uicolor into plist.Thanks in advance.Your suggestion is important for me.Please provide sample snippet rather than posting links.

You would need to store the individual colors separately (RGBA, if that is the color space you are using) in an array of NSNumbers.
NSNumber *red = [NSNumber numberWithFloat:components[0]];
NSNumber *green = [NSNumber numberWithFloat:components[1]];
NSNumber *blue = [NSNumber numberWithFloat:components[2]];
NSNumber *alpha = [NSNumber numberWithFloat:components[3]];
NSArray *colors = [NSArray arrayWithObjects:red, green, blue, alpha, nil];
The reason for this is that you can only add objects to a NSArray. A float or CGFloat is not an object.

Related

Color String in the Label

If I want to create some strings having format as these:
the string is the text property of a label.
some characters in this string have color different from other characters.
some characters are underlined and have a link, and when I hit the characters, and other views pop up.
Could somebody tell me how can I realize this effect?
If you support iOS < 6, better use 3rd party component e.g. TTTAttributedLabel. Else use attributedText property of UILabel.
NSDictionary *colors = [[NSDictionary alloc] initWithObjectsAndKeys: [UIColor blueColor], #"Hyd", [UIColor brownColor], #"Bang", [UIColor orangeColor], #"Delhi", [UIColor yellowColor], #"Gujarat", nil];
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:#""];
for (NSString *word in colors) {
UIColor *color = [colors objectForKey:word];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
NSAttributedString *substring = [[NSAttributedString alloc] initWithString:word attributes:attributes];
[attributeString appendAttributedString:substring];
}

Count values in NSDictionarys within an NSArray

I have an NSArray full of NSDictionary objects. Each dictionary object has a key:value:
color: red
Amongst other keys... color could be one of many colors that are user generated.
I would like to be able to extract each color and count how many I have of each, so:
red: 3 dictionaries
yellow: 4 dictionaries
etc... How exactly would this be possible? Just iterate through the dictionaries until you find a unique color, then search the array using NSPredicate again for that color, rinse, repeat?
You could just maintain a dictionary mapping color to the number of dictionaries with that color. For example:
NSMutableDictionary *counts = [NSMutableDictionary dictionary];
for (NSDictionary *dict in myArray) {
NSString *color = [dict objectForKey:#"color"];
NSNumber *val = [counts objectForKey:color];
val = [NSNumber numberWithUnsignedInteger:1+[val unsignedIntegerValue]];
[counts setObject:val forKey:color];
}

iPhone UIColor in Plist file

Is that possible to save colorcodes in Plist file somehow?
Do I have to represent them in string? and then can I create colors from them?
I saw a similar thread here about this but it has no answer yet
What could be done?
I can give you a suggestion. Instead of storing the color names you can store the RGB values in a array and store that array in each row in the plist.
key - Red
Type - Array
value - 1.0,0.0,0.0
Retrieve the array for each key.
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];
Just my thought..
UIColor (see here) conforms to the NSCoding protocol (see here) meaning you can write them out to a plist if you do it using NSCoding.
There is a great tutorial here about saving and restoring your app data using NSCoding
You can use NSKeyedArchiver which inherits from NSCoder
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[UIColor purpleColor]];
[[NSUserDefaults standardUserDefaults] registerDefaults:#{#"color": data}];
To get the color back out you would use NSKeyedUnarchiver:
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
NSLog(#"%#", [NSKeyedUnarchiver unarchiveObjectWithData: dict[#"color"]]);
To preserve human readability, 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].

Set a value for CGRect.frame.size from a NSDictionary

I generate a dictionary from a JSON string, and one of the value of the dictionary aims to be used as the height of a WebView object.
NSDictionary *dic = [jsonParser objectWithString:response];
CGFloat *height = [dic valueForKey:#"intHeightPixels"];
CGRect frame = webView.frame;
frame.size.height = height;
webView.frame = frame;
In line 2 I get the following error:
Initializing 'CGFloat' (aka float) with an expression of incompatible type 'id'.
I'm newbie in Objective C I don't know if this has something go see with pointers, casting, please give me some light.
you can take particular value in string i.e,
NSString *str=[arr valueForKey:#"location"];
CGRect rect9 = CGRectFromString(str);
This will convert to CGRect so that you can use it in the place of Frame
You might want to try CGFloat *height = [[dic valueForKey:#"intHeightPixels"] floatValue];
In dictionaries there is only object (here it's a NSNumber)

How to store enum values in a NSMutableArray

My problem is since an enum in objective-c essentially is an int value, I am not able to store it in a NSMutableArray. Apparently NSMutableArray won't take any c-data types like an int.
Is there any common way to achieve this ?
typedef enum
{
green,
blue,
red
} MyColors;
NSMutableArray *list = [[NSMutableArray alloc] initWithObjects:
green,
blue,
red,
nil];
//Get enum value back out
MyColors greenColor = [list objectAtIndex:0];
Wrap the enum value in an NSNumber before putting it in the array:
NSNumber *greenColor = [NSNumber numberWithInt:green];
NSNumber *redColor = [NSNumber numberWithInt:red];
NSNumber *blueColor = [NSNumber numberWithInt:blue];
NSMutableArray *list = [[NSMutableArray alloc] initWithObjects:
greenColor,
blueColor,
redColor,
nil];
And retrieve it like this:
MyColors theGreenColor = [[list objectAtIndex:0] intValue];
A modern answer might look like:
NSMutableArray *list =
[NSMutableArray arrayWithArray:#[#(green), #(red), #(blue)]];
and:
MyColors theGreenColor = ((NSInteger*)list[0]).intValue;
Macatomy's answer is correct. But instead of NSNumber I would suggest you use NSValue. That is its purpose in life.
NSMutableArray *corners = [[NSMutableArray alloc] initWithObjects:
#(Right),
#(Top),
#(Left),
#(Bottom), nil];
Corner cornerType = [corner[0] intValue];
You can wrap your enum values in a NSNumber object:
[NSNumber numberWithInt:green];
To go with NSNumber should be the right way normally. In some cases it can be useful to use them as NSString so in this case you could use this line of code:
[#(MyEnum) stringValue];