简体   繁体   中英

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 <-> thin for example)

thanks a lot for any help!

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.

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)

在此输入图像描述

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!!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM