UILabel的字体大小可以在iPhone上用stream畅的animation改变吗?

当在一些游戏菜单屏幕中select时,我想要一个UILabel稍微膨胀。 为了顺利resize,我认为我应该对animation块中的标签属性进行一些更改。

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

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

有没有其他的方法来做到这一点?

将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); }]; 

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

我在Swift中创build了UILabel扩展。 如果您的标签左alignment,请取消注释注释行。

 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 

如果你想从fontSize到fontSize的animation,你可以使用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) 

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

检查出来: https : //github.com/ivankovacevic/ARLabel

您不能更改字体的磅值,但可以使用新的UIFontreplaceUILabel的字体,该UIFont使用所需的新磅值。

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

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