在iPhone上可以用流畅的动画改变UILabel的字体大小吗?

wil*_*lc2 38 uikit uilabel iphone-sdk-3.0

我希望UILabel在某些游戏菜单屏幕中选择时稍微膨胀.为了顺利调整大小,我认为我应该对动画块中的标签属性进行一些更改.

要尝试的显而易见的事情是更改label.font.pointSize属性,但这只是readonly.

使用CGAffineTransformationMakeScale()缩放标签的.transform属性会使文本模糊.

还有其他方法可以做到这一点吗?

Bar*_*ley 57

将UILabel上的字体设置为放大时所需的大小.然后缩小它.如果您希望标签膨胀,请将其缩放至原始尺寸.

    messageLabel.font = [UIFont boldSystemFontOfSize:45]; 
    messageLabel.transform = CGAffineTransformScale(messageLabel.transform, 0.25, 0.25); 
    [self.view addSubview:messageLabel]; 
    [UIView animateWithDuration:1.0 animations:^{
        messageLabel.transform = CGAffineTransformScale(messageLabel.transform, 4, 4);
    }];
Run Code Online (Sandbox Code Playgroud)


mix*_*xel 12

更新了Xcode 8/Swift 3/iOS 10 SDK.

UILabel在Swift中创建了扩展.如果您的标签左对齐,请取消注释注释行.

import UIKit

extension UILabel {
    func animateToFont(_ font: UIFont, withDuration duration: TimeInterval) {
        let oldFont = self.font
        self.font = font
        // let oldOrigin = frame.origin
        let labelScale = oldFont!.pointSize / font.pointSize
        let oldTransform = transform
        transform = transform.scaledBy(x: labelScale, y: labelScale)
        // let newOrigin = frame.origin
        // frame.origin = oldOrigin
        setNeedsUpdateConstraints()
        UIView.animate(withDuration: duration) {
        //    self.frame.origin = newOrigin
            self.transform = oldTransform
            self.layoutIfNeeded()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Objective-C版本:

@interface UILabel(FontAnimation)

- (void) animateToFont:(UIFont*)font withDuration:(NSTimeInterval) duration;

@end

@implementation UILabel(FontAnimation)

- (void) animateToFont:(UIFont*)font withDuration:(NSTimeInterval) duration {
    UIFont * oldFont = self.font;
    self.font = font;
    // CGPoint oldOrigin = self.frame.origin;
    CGFloat labelScale = oldFont.pointSize / font.pointSize;
    CGAffineTransform oldTransform = self.transform;
    self.transform = CGAffineTransformScale(self.transform, labelScale, labelScale);
    // CGPoint newOrigin = self.frame.origin;
    // self.frame = CGRectMake(oldOrigin.x, oldOrigin.y, self.frame.size.width, self.frame.size.height);
    [self setNeedsUpdateConstraints];
    [UIView animateWithDuration:duration animations: ^{
        // self.frame = CGRectMake(newOrigin.x, newOrigin.y, self.frame.size.width, self.frame.size.height);
        self.transform = oldTransform;
        [self layoutIfNeeded];
    }];
}

@end
Run Code Online (Sandbox Code Playgroud)


obe*_*ber 5

如果您想要从fontSize动画到fontSize,可以使用CATextLayer类

// create text layer 
let textLayer = CATextLayer()
textLayer.font = UIFont.systemFontOfSize(50)
textLayer.fontSize = 50
textLayer.string = "text"
textLayer.foregroundColor = UIColor.redColor().CGColor
textLayer.backgroundColor = UIColor.blackColor().CGColor
textLayer.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
view.layer.addSublayer(textLayer)

// animation
let animation = CABasicAnimation(keyPath: "fontSize")
animation.toValue = 10
animation.duration = 3
textLayer.layer.addAnimation(animation, forKey: nil)
Run Code Online (Sandbox Code Playgroud)