Seb Seb - 1 year ago 71
iOS Question

Briefly change colors in a UILabel

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download