The question was asked long ago, but here goes.
As said above, textColor for UILabel
is not animate-able. A useful trick is to dynamically create in code another UILabel
, with the same attributes and position, but with a destination color. You animate the alpha of the new UILabel
from 0.0 to 1.0, so it appears like the textColor of the original UILabel is animated. You can remove one of the labels when the animation is completed.
Here is an example of a class level method that changes to a different textColor for a short while and changes it back.
+(void)colorizeLabelForAWhile:(UILabel *)label withUIColor:(UIColor *)tempColor animated:(BOOL)animated
{
// We will:
// 1) Duplicate the given label as a temporary UILabel with a new color.
// 2) Add the temp label to the super view with alpha 0.0
// 3) Animate the alpha to 1.0
// 4) Wait for awhile.
// 5) Animate back and remove the temporary label when we are done.
// Duplicate the label and add it to the superview
UILabel *tempLabel = [[UILabel alloc] init];
tempLabel.textColor = tempColor;
tempLabel.font = label.font;
tempLabel.alpha = 0;
tempLabel.textAlignment = label.textAlignment;
tempLabel.text = label.text;
[label.superview addSubview:tempLabel];
tempLabel.frame = label.frame;
// Reveal the temp label and hide the current label.
if (animated) [UIView beginAnimations:nil context:nil];
tempLabel.alpha = 1;
label.alpha = 0;
if (animated) [UIView commitAnimations];
// Wait for while and change it back.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, FOR_AWHILE_TIME*NSEC_PER_SEC), dispatch_get_main_queue(), ^{
if (animated) {
// Change it back animated
[UIView animateWithDuration:0.5 animations:^{
// Animate it back.
label.alpha = 1;
tempLabel.alpha = 0;
} completion:^(BOOL finished){
// Remove the tempLabel view when we are done.
[tempLabel removeFromSuperview];
}];
} else {
// Change it back at once and remove the tempLabel view.
label.alpha = 1.0;
[tempLabel removeFromSuperview];
}
});
}