0

Is it possible to detect the colour of a particular UILabel programatically?

The intention for these lines of code, is to change the background from a red colour, to a white colour. Increase the brightness for a brief second then reduce the brightness and follow up with a label background colour back to red.

The issue is that sometimes, the brightness will increase without the background colour changing. Thus resulting in a major flaw in the design for the application. Which is private.

    self.label.backgroundColor = [UIColor colorWithRed:255.0/255 green:255.0/255 blue:255.0/255 alpha: 1.0];
    [UIScreen mainScreen].brightness = 1.0;
    [self performSelector:@selector(resetColor) withObject:self afterDelay:0.05f ];

- (void)resetColor {
    [UIScreen mainScreen].brightness = 0.0;
    self.label.backgroundColor = [UIColor colorWithRed:255.0/255 green:0.0/255 blue:0.0/255 alpha:0.5];

Can anyone see why it would randomly not change the background colour beforehand?

And mainly, is it possible to do the following?

if (self.label.backgroundColor == [UIColor whiteColor]){ or while (self.label.backgroundColor == [UIColor whiteColor){

?

jww
  • 97,681
  • 90
  • 411
  • 885
user2415313
  • 101
  • 1
  • 10
  • Comparisons should be done using `[obj1 isEqual:obj2]` not using the `==` operator. – entropid Jan 30 '15 at 02:10
  • @entropid So it can be done, excellent. How about the colour not changing prior to the brightness increase? Is there an animation time I'm not taking into consideration? – user2415313 Jan 30 '15 at 02:11
  • The colour should change straight away. It's odd that it doesn't happen. – entropid Jan 30 '15 at 02:15
  • Do you use multithreading at all? – WolfLink Jan 30 '15 at 02:15
  • @entropid It's quite noticable and in a dark room this is tending to give a light red glow as opposed to a white glow. But hopefully I can figure something out. – user2415313 Jan 30 '15 at 02:16
  • @WolfLink I'm not sure I'm that advanced to know whether I'm using that or not. Is multithreading capable on all devices? – user2415313 Jan 30 '15 at 02:17
  • It sounds like you're trying to **animate** the color change. If so, see "[How to animate the background color of a UILabel?](http://stackoverflow.com/q/3762561)" – jscs Jan 30 '15 at 02:18
  • Try to use a longer delay and see it that changes anything. – entropid Jan 30 '15 at 02:18
  • @entropid I can't use a longer delay unfortunately. It ruins the purpose of the function. However, increasing the duration does fix the problem and it achieves full white brightness. However, it has to perform at this duration or less to complete its task. – user2415313 Jan 30 '15 at 02:21
  • @JoshCaswell There aren't any animations set, so I'm assuming it has some sort of noticeable delay when we're talking 0.05 of a second. – user2415313 Jan 30 '15 at 02:21
  • 1
    @user2415313 multithreading is possible on all iOS devices, even those that are not multi-core. If you aren't using it intentionally, you probably aren't using it at all, but just to be safe, try setting the background color with `[self.label performSelectorOnMainThread:@selector(setBackgroundColor:) withObject:[UIColor whiteColor] waitUntilDone:true];` – WolfLink Jan 30 '15 at 02:22
  • 1
    That could be the problem. The colours isn't changed immediately but queued and then dispatched. Your delay is far too low and probably the app hasn't got the time to execute the change that you change it back. – entropid Jan 30 '15 at 02:22
  • @WolfLink I tried the code you provided and I'm still getting the same results unfortunately. However, thanks for enlightening me on that possible path. – user2415313 Jan 30 '15 at 02:25
  • 1
    @entropid I think the only option is to overlay two labels and try a hidden to see if it can perform it quicker. Thanks for your help. – user2415313 Jan 30 '15 at 02:26
  • Also it is possible to use `self.label.backgroundColor == [UIColor whiteColor]` if the background color was set using `[UIColor whiteColor]` but if it was set using `[UIColor colorWithRed:1 blue:1 green:1 alpha:1]` then you will need to use `[self.label.backgroundColor isEqual:[UIColor whiteColor]` – WolfLink Jan 30 '15 at 02:30
  • 50 milliseconds is short; that's pretty much the lower limit for timing things in Cocoa without using a dedicated animation feature. – jscs Jan 30 '15 at 02:31
  • Thanks for the answers guys, much appreciated. I'll just have to try and work with what I've got. So long as it's consistent there's no issue, only if it's inconsistent. – user2415313 Jan 30 '15 at 02:49

1 Answers1

0

your function:

[self performSelector:@selector(resetColor) withObject:self afterDelay:0.05f ];

which of this function called?

to compare the color are extactly the same: use isEqual: instead of == i.e.

if ([self.label.backgroundColor isEqual:[UIColor whiteColor]])

otherwise, you should compare the all of Color's elements, i.e. RGBA

Steve Lai
  • 633
  • 1
  • 7
  • 18
  • it calls for the resetColor part of the code I provided. Thank you for the isEqual, I'm glad this can be achieved. – user2415313 Jan 30 '15 at 02:22