Comparing text color in UILabel with UIColor error

I am trying to compare the text color in UIlabel with UIColor, but the result is always false.

The following code gives the result:

color equal 1, 0

I expect a and b to be equal to 1. Is there any other way to make this comparison?

    bool a,b;

    UIColor *myColor1, *myColor2;

    myColor1 = [UIColor redColor];
    mainViewController.timerLabel.textColor = [UIColor redColor];

    myColor2 = [UIColor colorWithCGColor:mainViewController.timerLabel.textColor.CGColor];


    a = [[UIColor redColor] isEqual:myColor1];
    b = [[UIColor redColor] isEqual:myColor2];

    NSLog(@"color equal %i, %i",a,b);
+5
source share
1 answer

UIColor does not define isEqual, isEqual inherits from NSObject. Thus, isEqual compares color addresses and failure.

CGColor has a comparison function CGColorEqualToColor():

CGColor *c = myColor.CGColor;

Then you can compare the colors of CGColor:

bool colorsEqual = CGColorEqualToColor(myColor1.CGColor, myColor2.CGColor);

Or get the individual components of two colors and compare them separately using - (BOOL)getRed:(CGFloat *)red green:(CGFloat *)green blue:(CGFloat *)blue alpha:(CGFloat *)alpha

+11
source

All Articles