I have a pickerView which has a component of colors.
I have a textField (textOther), now I have NSMutableArray of all the available colors which are explicitly defined.
How do I pass all those colors(which are strings!) to these textField?
textField setTextColor will set only a single color, but to pass string to a color how do I do it?
Thanks!
I suppose the cleanest way to do this would be by making an NSDictionary
with predefined UIColor
instances, and map them as key value pairs; keys being the string that represents the color, value being the actual UIColor
object.
So if you'd want to have both red and blue as possible colors (which are now in an NSMutableArray
if I understood correctly), you'd make an NSDictionary
like this:
NSArray * colorKeys = [NSArray arrayWithObjects:@"red", @"blue" nil];
NSArray * colorValues = [NSArray arrayWithObjects:[UIColor redColor], [UIColor blueColor], nil];
NSDictionary * colorMap = [NSDictionary dictionaryWithObjects:colorValues forKeys:colorKeys];
If you then want to pass a color (given a string AKA the key of said color), you can simply do this:
NSString * chosenColor = textField.text; // extract desired color from input in textField
textField.textColor = [colorMap valueForKey:chosenColor]; // use this color string to find the UIColor element in the colorMap
Hope this answers your question!