Animate change font of a UILabel
Asked Answered
U

5

10

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).

Uplift answered 27/6, 2013 at 11:29 Comment(0)
L
14

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.

Luxury answered 27/6, 2013 at 11:35 Comment(0)
S
5

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

Soapbark answered 9/3, 2019 at 10:16 Comment(0)
C
-1

You can set new size and make changes to your label after animation like this.

[UIView animateWithDuration:0.9 animations:^{
        label.frame = (CGRect){ CGPointMake(51, 150), label.bounds.size };
    } completion:^(BOOL finished) {
        label.font=[UIFont boldSystemFontOfSize:15];
        // etc.
    }];

Enjoy!!

Conoscenti answered 27/6, 2013 at 11:36 Comment(1)
Not really.. i wanted to animate the font transition.. this just changes it at the end of the animation.Uplift
F
-2

Use uiview animateWithDuration. Inside the block change the font type (bold)

Fearfully answered 27/6, 2013 at 11:34 Comment(0)
U
-3
if(font_index == 8)
    font_index = 0;

[UIView animateWithDuration:2
                      delay:0
                    options:UIViewAnimationOptionCurveLinear
                 animations:^{


                     UIFont* newfont = Nil;
                     if(font_index % 2 != 0)
                     {
                         newfont = [UIFont fontWithName:self.fontNames[font_index] size:8];

                     }
                     else
                     {
                         newfont = [UIFont fontWithName:self.fontNames[font_index] size:20];

                     }
                     self.keyButton.titleLabel.font = newfont;

                 }
                 completion:^(BOOL finished){
                    // if(finished)
                    // {
                         font_index++;
                         [self fontIt];
                     //}
                 }];

}

This code might answer your question

Uno answered 23/2, 2014 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.