Search code examples
xcodeios5textcoloruser-preferences

xcode 4 / ios5 - how to set textColor based on prefs


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


Solution

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