How to judge the color of UILabel?
UILabel *templabel = [self.wallBoxArray objectAtIndex:i];
for( int i = 0 ; i < [self.wallBoxArray count]; i++)
{
if(templa开发者_StackOverflowbel.backgroundColor == [UIColor greenColor])
{
NSLog(@"the color isn green");
}
}
There are many label's in my array. They all initialized with green color. But i judged that way ,why cant print " the color isn't green.
The UIColor class cluster implements -isEqual:
, so you could just use
if([templabel.backgroundColor isEqual:[UIColor greenColor]])
...
You are performing a pointer comparision there, so if the color's are both green, but different instances of UIColor, this will fail. And they are because UIView's backgroundColor property is a copy property.
@property(nonatomic, copy) UIColor *backgroundColor
I'm sort of surprised this is that convoluted, but to check for equality, try the following:
CGColorEqualToColor([templabel.backgroundColor CGColor], [[UIColor greenColor] CGColor])
This is checking equality of the color value, not just a pointer comparison. Also remember to do [str compare:otherString] == NSOrderSame
when checking strings!
精彩评论