Question

I have looked on SO but cannot find an answer that works for me, so here goes:

I am trying to set color, font and size elements based on user preferences. I have no problem with font and font size, but I cannot seem to get the color to work.

This is what I'm doing:

  1. I have a group of constants, like:

    asterisk define kBlackColor @"[UIColor blackColor]"

  2. In reading my prefs file, I've determined that I want the blackColor, and set

    txtColor = kBlackColor;

txtColor is defined as:

NSString *txtColor;
  1. When I want to use txtColor, I'm writing this:

    cell.textLabel.textColor = (UIColor *) txtColor;

This compiles, but gives me an "unrecognized selector sent" message.

I would appreciate knowing the right way to go about this..

Was it helpful?

Solution 2

I decided to come at the problem a different way, so I should answer my own question - I hope it helps someone.

Instead of allowing users to change each setting, I created a series of styles (0-5), stored (among other things) in a plist.

Each style has a set of variables (font name, size, color, etc).

The settings file uses variables as shown below and is read when the app is started:

AppDelegate *mainDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
styleKeyValue = [mainDelegate styleValueKey];   

styleKey = [styleKeyValue intValue];

switch (styleKey) {

        case 0:
            fontName = @"Arial"; 
            fontSize = 16;
            selectedTintColor = [UIColor blackColor];
            selectedFontColor = [UIColor blackColor];
            backgroundImage = @"background0.png";
            break;


        case 1:
            fontName = @"Times"; 
            fontSize = 14;
            selectedTintColor = [UIColor blueColor];
            selectedFontColor = [UIColor blackColor];
            backgroundImage = @"background1.png";
            break;

        case 5:
            ...


    }

selectedTintColor and selectedFontColor are defined as:

UIColor *selectedTintColor;
UIColor *selectedFontColor;

fontName and backgroundImage are defined as NSStrings. font size is a local integer.

When I want to style a cell, I only have to enter this:

cell.textLabel.font = [UIFont fontWithName:fontName size:fontSize];
cell.textLabel.textColor = selectedFontColor;

(The tintColor is used to style segmentedCells.)

Again, I hope this helps someone. It took me all night to get to this rather simple solution..

OTHER TIPS

When you store txtColor, you are storing a string (forget about the fact that it looks like objective-c, you stored it as a string, and once stored as a string how could the compiler ever make use of it?), and there is no way to cast a string to a UIColor. How could there be?

To store colors into NSUserDefaults you have to serialize them as NSData.

To borrow from a previous answer:

Storing the color:

NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"myColor"];

Retrieving the color:

NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myColor"];
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top