39

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

显而易见的尝试是更改label.font.pointSize属性,但这是只读的。

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

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

4

5 回答 5

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);
    }];
于 2011-12-21T16:23:37.590 回答
14

针对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()
        }
    }
}

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
于 2015-09-29T20:44:01.163 回答
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)
于 2016-05-03T14:22:25.550 回答
2

我刚刚为此做了一个解决方案。它也是基于 CGAffineTransformScale 的,但它还有一些其他技巧可以处理所有边界情况:

看看: https ://github.com/ivankovacevic/ARLabel

于 2013-06-16T12:00:02.487 回答
-1

您无法更改字体的磅值,但可以将 UILabel 的字体替换为使用所需新磅值的新 UIFont。

label.font = [UIFont fontWithName:@"Arial-BoldMT" size:12.0];
// ... some time later
label.font = [UIFont fontWithName:@"Arial-BoldMT" size:14.0];

您可能还需要修改 UILabel 的框架以适应新的磅值,尤其是在您增加磅值的情况下。否则,您的标签将被裁剪。

于 2010-09-30T04:24:15.017 回答