Animating UILabel Font Size Change

前端 未结 9 2067
醉话见心
醉话见心 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:39

    I've created UILabel extension in Swift.

    import UIKit
    
    extension UILabel {
        func animate(font: UIFont, duration: TimeInterval) {
            // let oldFrame = frame
            let labelScale = self.font.pointSize / font.pointSize
            self.font = font
            let oldTransform = transform
            transform = transform.scaledBy(x: labelScale, y: labelScale)
            // let newOrigin = frame.origin
            // frame.origin = oldFrame.origin // only for left aligned text
            // frame.origin = CGPoint(x: oldFrame.origin.x + oldFrame.width - frame.width, y: oldFrame.origin.y) // only for right aligned text
            setNeedsUpdateConstraints()
            UIView.animate(withDuration: duration) {
                //L self.frame.origin = newOrigin
                self.transform = oldTransform
                self.layoutIfNeeded()
            }
        }
    }
    

    Uncomment lines if the label text is left or right aligned.

提交回复
热议问题