How to animate the textColor property for UILabel?

For some reason, when I try to animate textColor, this will not work. TextColor just suddenly changes from A to B. Is it possible to revive it, from let say red to black?

+76
iphone core-animation
Mar 11 '10 at 16:14
source share
9 answers

The textColour property is not indicated to be actionable in documents, so I don't think you can do this with a simple UIView animation block ...

This can be done rather roughly if NSTimer fires every few milliseconds, each time setting the color gradually from one to the other.

I say this is rude because it would require an array or some other container with predefined color values ​​that go from the initial color to the finish color, and I'm sure you can do this using basic animation or something else I just don’t know what it is.

+4
Mar 11 '10 at 16:22
source share

Instead, if you tried using a crossfade transition on the object itself, like this, it will give you a nice fade effect from one color to another:

 [UIView transitionWithView:myLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ myLabel.textColor = NEW_COLOR; } completion:^(BOOL finished) { }]; 

This is better than using NSTimers, CATextLayers, etc. etc. for various reasons. CATextLayer does not have proper text kerning or NSAttributedText support, and NSTimers does not have lags (plus too much code). The above transition animation does the trick, and can also be used in chain animation. I had the same problem, and I already tried the solutions above, but this simple code works wonders.

+170
Jan 2 '14 at 10:13
source share

Swift 4 solution:

 UIView.transition(with: yourLabel, duration: 0.3, options: .transitionCrossDissolve, animations: { self.yourLabel.textColor = .red }, completion: nil) 
+51
Jul 18 '16 at 12:36
source share

The reason that textColor is not animated is because UILabel uses a regular CALayer instead of a CATextLayer.

To make textColor animatable (as well as text, font, etc.), we can subclass UILabel to use CATextLayer.

This is quite a bit of work, but fortunately I have already done this :-)

You can find the full explanation + open source replacement for UILabel in this article

+12
Oct 23 2018-11-11T00:
source share

You can try creating another instance of UILabel or whatever that has textColor, and then apply the animation between the two instances (with the old textColor and the one who has the new textColor).

+9
Mar 11 '10 at 16:54
source share

It was ONLY the thing that worked for me:

 let changeColor = CATransition() changeColor.duration = 1 CATransaction.begin() CATransaction.setCompletionBlock { selected.label.layer.addAnimation(changeColor, forKey: nil) selected.label.textColor = .blackColor() } selected.nameLabel.textColor = .redColor() CATransaction.commit() 
+7
Jun 26 '18 at 11:54
source share

Here is my code for animating the text color of the label. The important part is setting textColor to clear the color before animation

 label.textColor = [UIColor clearColor]; [UIView transitionWithView:label duration:duration/4 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ label.textColor = self.highlightedCellPrimaryTextColor; } completion:^(BOOL finished) { }]; 
+6
Jun 03
source share

It was pretty easy for me to place the UIView under the label and animate its opacity. A label must be added to this view. This may not be a good solution in terms of resource consumption, but quite simple.

0
Sep 03 '13 at 11:44
source share

updated swift 3.0

I just changed from @budidino comments

 UIView.transition(with: my.label, duration: 0.3, options: .transitionCrossDissolve, animations: { my.label.textColor = .black }, completion: nil) 
-one
Feb 09 '17 at 14:37
source share



All Articles