Question

Sounds simple, right? Well I've Googled over a dozen search strings and still cannot find the answer.

To set the colour we implement the following code:

[_myUIButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

But what about testing to see what the colour is of the text of the button??? Surely this is possible to do, right?

Shouldn't the code be something like this:

if([_myUIButton.textColor isEqual:[UIColor blueColor]])
{
    // then true
}

This code won't work with the button title text colour, but someone must know how it can be done.

Please help.

Was it helpful?

Solution

if([button titleColorForState:UIControlStateNormal] isEqual:[UIColor blackColor]]){

  }

OTHER TIPS

When in doubt, check the Apple docs.

- (UIColor *)titleColorForState:(UIControlState)state;

You can use UIButton's currentTitleColor property:

if ([button.currentTitleColor isEqual:[UIColor blueColor]]) {
    // Do something
}

NSLog(@"Color is %@",[btn currentTitleColor]); this will give you the answer 0 0 0 1 for black color it means red is 0 green is 0 and blue is 0 and alpha is 1,this way you find the RGB of any color ,alternatively you can use if condition to match a specific color

you also can use

if([myButton.titleLabel.textColor  isEqual:[UIColor blueColor]]){
    // your code
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top