Briefly change colors in a UILabel

Go To


I have a label that gets updated with values calculated from information received by the server. Depending on how much traffic there is the updates can come in rapidly or at random intervals. What I would like to do is compare the new calculated value with the old value and if it has increased then change the text to be green and the background to be a darker green and if the value has decreased then change the text to a shade of red and dark red.

That part is easy to do, what I am having some trouble is after a half a second or so I would like to change the background and text to be their default colors. I'm doing this as part of user feedback for when the values change.

Any information would be greatly appreciated.

2012-04-04 18:19
by Seb


A label is a subclass of UIView. Assuming you have some method that notifies you of when the change occurs...

- (void)someMethodThatNotifiesOfChange {
    // Calculate new values and assume the result means color button green
    [UIView animateWithDuration:0.5 animations:^{
         label.backgroundColor = [UIColor greenColor];
    } completion:^(BOOL finished) {
         if (finished) {
             [UIView animateWithDuration:0.5 delay:5.0 options:nil animations:^{
                   label.backgroundColor = [UIColor clearColor];
         } completion:nil];


This just changes the background to be green, but illustrates the point none-the-less. This take 0.5 seconds to animate to green. Then, when it is complete, it waits 5 seconds and then takes 0.5 to animate back to clear.

2012-04-04 18:29
by jmstone617
This worked beautifully for me in the end. Thank you so much for the help - Seb 2012-04-05 13:32
@jmstone i tried what you said and it works for me except that it doesn't wait for the delay. any help would be appreciate - Kijit Desai 2015-07-27 22:34
What is happening? It turns green in 0.5s and then clear in 0.5s - jmstone617 2015-07-28 15:27


You can use CoreAnimation library if you want advanced animation but if you just animate UILabel properties, you can use UIView static method to do this. You can do it like this:

[UIView animateWithDuration:0.3f animations:^{
  label.textColor = [UIColor greenColor];
} completion:^(BOOL finished) {
  [UIView animateWithDuration:0.3f animations:^{
    label.textColor = [UIColor blackColor];
  } completion:nil];

In first animation block, you set label's text color (and background color if you want). In its completion block you set the label's color back using another animation.

Hope it helps.

2012-04-04 18:31
by Martin Pilch
Almost a great answer. The second animation needs to involve the delay, otherwise as soon as the label turns green it'll go to black without remaining at green for any amount of time. Thus, my answer below is a better implementation for this specific scenario. Still a good answer though - jmstone617 2012-04-04 18:34
You are absolutely right. Thank you for correction - Martin Pilch 2012-04-04 19:58


Take a look at NSTimer. Timers are useful to lots of situations!

Good luck!

2012-04-04 18:25
by Zalykr


Alternatively by IOS 4 the followwing block would do the same effect

    [UIView transitionWithView: infoLabelInternet duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        infoLabelPremium.textColor = kPopoverActiveTextColor;
    } completion:nil];
2012-11-28 14:37
by Ilker Baltaci


You can also do it this way:

label.backgroundColor = [UIColor greenColor];
[label performSelector:@selector(setBackgroundColor:) withObject:[UIColor clearColor] afterDelay:0.5];
2016-09-07 13:31
by Joseph El Khoury