views:

204

answers:

1

I'm trying to compare the text colour in a UIlabel with a UIColor but the result is always false.

The following code produces the result:

color equal 1, 0

I expect both a and b to be equal to 1. Is there another way to do this compare?

 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  A: 

UIColor does not define isEqual, isEqual is inheritd from NSObject. Thus isEqual is comparing the addresses of the colors and will fail.

UIColor is basically a one-way-street. The only way to get the color components out is:

CGColor *c = myColor.CGColor;

Then the CGColor colors can be compared:

bool colorsEqual = CGColorEqualToColor(myColor1.CGColor, myColor2.CGColor);
zaph
Thanks Zaph, that makes sense.
Joe