Animating UILabel Font Size Change

前端 未结 9 2033
醉话见心
醉话见心 2020-11-30 21:31

I am currently making an application that uses a custom View Controller container. Multiple views are on the screen at one time and when one is tapped, the selected view con

相关标签:
9条回答
  • 2020-11-30 21:54

    You can change the size and font of your UILabel with animation like bellow .. here I just put the example of how to change the font of UILabel with transform Animation ..

        yourLabel.font = [UIFont boldSystemFontOfSize:35]; // set font size which you want instead of 35
        yourLabel.transform = CGAffineTransformScale(yourLabel.transform, 0.35, 0.35); 
        [UIView animateWithDuration:1.0 animations:^{
            yourLabel.transform = CGAffineTransformScale(yourLabel.transform, 5, 5);
        }];
    

    I hope this helpful to you..

    0 讨论(0)
  • 2020-11-30 21:56

    You could also use CATextLayer which has fontSize as an animatable property.

    let startFontSize: CGFloat = 20
    let endFontSize: CGFloat = 80
    
    let textLayer = CATextLayer()
    textLayer.string = "yourText"
    textLayer.font = yourLabel.font.fontName as CFTypeRef?
    textLayer.fontSize = startFontSize
    textLayer.foregroundColor = UIColor.black.cgColor
    textLayer.contentsScale = UIScreen.main.scale //for some reason CATextLayer by default only works for 1x screen resolution and needs this line to work properly on 2x, 3x, etc. ...
    textLayer.frame = parentView.bounds
    parentView.layer.addSublayer(textLayer)
    
    //animation:
    let duration: TimeInterval = 1
    textLayer.fontSize = endFontSize //because upon completion of the animation CABasicAnimation resets the animated CALayer to its original state (as opposed to changing its properties to the end state of the animation), setting fontSize to endFontSize right BEFORE the animation starts ensures the fontSize doesn't jump back right after the animation.
    let fontSizeAnimation = CABasicAnimation(keyPath: "fontSize")
    fontSizeAnimation.fromValue = startFontSize
    fontSizeAnimation.toValue = endFontSize
    fontSizeAnimation.duration = duration
    fontSizeAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    textLayer.add(fontSizeAnimation, forKey: nil)
    

    I used it in my project: https://github.com/yinanq/AngelListJobs

    This animation keeps the font top left aligned (unlike CGAffineTransformScale scaling the label from center), pro or con depending on your needs. A disadvantage of CATextLayer is that CALayers don't work with autolayout constraint animation (which I happened to need and solved it by making a UIView containing just the CATextLayer and animating its constraints).

    0 讨论(0)
  • 2020-11-30 22:00

    For those not looking for a transform, but actual value change:

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

    0 讨论(0)
提交回复
热议问题