iOS Dev Nugget 273 Animating UILabel textColor Changes


Need to run a code review on your codebase? Hire me

In UIView and its subclasses, there are many properties — such as frame and alpha — that can be animated when they change. i.e. you can do something this and changes will be animated:

UIView.animate(withDuration: 2) {
    self.label.frame = CGRect(x: 150, y: 200, width: 50, height: 20)
    self.button.tintColor = .white

Unfortunately, this doesn't work for UILabel's color property. Here's the good news: You can approximate this using transition(with:duration:options:animations:completion:) instead:

UIView.transition(with: label, duration: 2, options: .transitionCrossDissolve, animations: {
    self.label.textColor = .red
}, completion: nil)

Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No


Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter


View archives of past issues