Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate change font of a UILabel

I need a way to change UILabel's font with an animation. I saw many ways to animate changes in font size but I need a way to change it's type (bold ti thin for example).

like image 678
Chiko Avatar asked Jun 27 '13 11:06

Chiko


2 Answers

This might be helpful to people who search this answer:

To fade from one font to another do this:

UIView.transition(with: label, duration: 0.25, options: .transitionCrossDissolve, animations: {
    self.label.font = UIFont.systemFont(ofSize: 15)
}) { isFinished in }

when there is text go to:

UIView.transition(with: label, duration: 0.25, options: .transitionCrossDissolve, animations: {
    self.label.font = UIFont.boldSystemFont(ofSize: 15)
}) { isFinished in }

(Gif shows different font)

enter image description here

like image 140
SirRupertIII Avatar answered Nov 15 '22 05:11

SirRupertIII


There's no discrete mapping (in the mathematical sense) between two font faces.

If you can go from size 10 to size 11 by ramping (10.1, 10.2, 10.3, ...) there's no such thing as "something 45% between helvetica neue and helvetica neue bold".

The closest you could do is morph between the CGPaths of the individual letters, but that'd involve a huge work with CoreText.

Meanwhile, I just advise you to perform a simple crossfade.

Maybe in the future (wink, wink) there will be some frameworks to help you with this kind of task.

like image 21
Cyrille Avatar answered Nov 15 '22 05:11

Cyrille