开发者

How to judge the color of UILabel?

开发者 https://www.devze.com 2022-12-27 14:42 出处:网络
UILabel *templabel = [self.wallBoxArray objectAtIndex:i]; for( int i = 0 ; i < [self.wallBoxArray count]; i++)
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!

0

精彩评论

暂无评论...
验证码 换一张
取 消