Singh
Singh

Reputation: 71

Set UIButton background colour using name of colour in iPhone

I am getting a json response from server. In that response I received an array of colour names in which colour name is like "Piel / Nude", "Negro / Black".

I would like to set this colour as UIButton's background colour. I tried this way but app crashes every time:-

button.backgroundColor = [UIColor performSelector:NSSelectorFromString(@"Black")];

Can anyone help me.

Upvotes: 0

Views: 507

Answers (4)

Kanhaiya
Kanhaiya

Reputation: 249

Solution #1 You should have a NSString with for example whiteColor or blueColor and you can convert it in the following way:

- (void)viewDidLoad {
[super viewDidLoad];
NSString *colorName=@"Yellow";
NSString *colorStr=[colorName lowercaseString];
colorStr=[NSString stringWithFormat:@"%@Color",colorStr];
UIColor *color=[self getColor:colorStr];
Yourbutton.backgroundColor=color;

}

-(UIColor *)getColor:(NSString*)colorStr
{
    SEL getColor = NSSelectorFromString(colorStr);
    UIColor *color = nil;
    if ( [UIColor respondsToSelector:getColor] == YES) {

        color = [UIColor performSelector:getColor];
    }
    return color;
}

Solution #2

    NSString *colorStr = @"red";

        NSString *selectorString = [colorStr stringByAppendingString:@"Color"];
        SEL selector = NSSelectorFromString(selectorString);
        UIColor *color = [UIColor blackColor];
        if ([UIColor respondsToSelector:selector]) {
            color = [UIColor performSelector:selector];
        }

        Yourbutton.backgroundColor=color;

Hope this answers your question!

Upvotes: 2

anatoliy_v
anatoliy_v

Reputation: 1800

Solution #1

You can add category for UIColor that will return color by it's name. Right now UIColor has class methods: [UIColor blackColor], [UIColor orangeColor] etc. If you don't like it you can add additional class method declarations:

// category of UIColor
+ (UIColor *)Black { return [UIColor blackColor]; }
+ (UIColor *)Green { return [UIColor greenColor]; }
+ (UIColor *)MyBestColor { return [UIColor colorWithRed:0.5 green:0.8 blue:0.1 alpha:1.0]; }

In this case your following code will work

button.backgroundColor = [UIColor performSelector:NSSelectorFromString(@"Black")];

Solution #2

It's more complex way. If you want to have all colors even those that are not included in UIColor's declaration you can do following:

1) Collect color names and hex (or RGB) values. You can parse data from here Web Colors or find other source. Store it in the array in plist file in your project. 2) Add class method that will find color value by it's name. Something like this:

+ (UIColor *)colorByName:(NSString *)name {
    // Here you will parse your plist file, find color value by it's name and create a color. 
}

To create UIColor from HEX string you may use this project HexColor

Upvotes: 1

user4226071
user4226071

Reputation:

If there is chance to have many colours, it will be better to update the json response from server. Instead of getting the colour name, get the RGB & Alpha value from the server; like,

{ "red" :"0.0", "green" : "0.5", "blue" :"0.2", "alpha" :"1.0" }

Then you can use,

[UIColor colorWithRed:red green:green blue:blue alpha:alpha];

Upvotes: 0

Fabio Felici
Fabio Felici

Reputation: 2916

UIColor offers you many default color using + (UIColor *)colorNameColor but not all colors that you may need. So one way to solve this is to crate a category on UIColor and declare the class method that you need like + (UIColor *)pielColor or + (UIColor *)nudeColor and use RGB value:

+ (UIColor *)pielColor
{
    return [UIColor colorWithRed:1.0 green:0.8 blue:0.6 alpha:1.0];
}

Then use:

NSString *methodName = [NSString stringWithFormat:@"%@Color", [yourColor lowercaseString]];
[UIColor performSelector:NSSelectorFromString(methodName)];

Upvotes: 0

Related Questions