if([button titleColorForState:UIControlStateNormal] isEqual:[UIColor blackColor]]){
}
How to check the colour of the text on a UIButton?
-
27-06-2022 - |
質問
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.
解決
他のヒント
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
}