Using a NSString to set a color for a label

I want to create a method and use a string value “redColor” to set the UIColor for a label. tableColorName is the NSString “redColor” and I tried to apply a selector to create the UIColor and apply it to my textLabel. Thanks

-(void) changeLabelColor
    SEL labelColor = NSSelectorFromString([NSString stringWithFormat:[@"%@", tableColorName]]);

    UIColor *color = [[UIColor class] performSelector:labelColor];
    self.textLabel.textColor = color;

  • How to I properly set UIColor from int?
  • SKSpriteNode setting color with UIColor
  • iOS - Setting UIButton background color only colors the corners
  • How do I comfortably use CGColor in an NSArray with CGGradient
  • Generate a random UIColor
  • SearchBar, how to change text color?
  • Solutions Collect From Internet About “Using a NSString to set a color for a label”

    Use this method

    -(UIColor *)giveColorfromStringColor:(NSString *)colorname
        SEL labelColor = NSSelectorFromString(colorname);
        UIColor *color = [UIColor performSelector:labelColor];
        return color;

    Call as

    [view setBackgroundColor:[self giveColorfromStringColor:@"redColor"]];

    The method name takes colorname as input and gives the corresponding UIColor

    Thus in your case the call will be

    self.textLabel.textColor = [self giveColorfromStringColor:@"redColor"];