animationUILabel字体大小更改

我目前正在使用自定义视图控制器容器的应用程序。 屏幕上同时显示多个视图,当点击一个视图时,所选视图控制器将全屏显示。 这样做,选定的视图控制器子视图缩放比例(框架,字体大小等)虽然,UILabel的字体属性是不可animation导致的问题。 我已经尝试了多个解决scheme,但所有平坦的吸。

我试过的解决scheme是:

  1. 截取较大视图的截图并对更改进行animation处理(与Flipboard的工作方式类似)
  2. 通过使用transform属性进行animation处理
  3. 缩小UIScrollView并在全屏显示时放大。
  4. 设置adjustsFontSizeToFitWidth为YES,并在animation之前设置fontSize

其中一个是迄今为止最好的解决scheme,但我不满意。

我正在寻找其他的build议,如果任何人有任何或UILabel取代animation顺利使用[UIViewanimation..]。

这是一个很好的例子,类似于我想要我的UILabel: http : //www.cocoawithlove.com/2010/09/zoomingviewcontroller-to-animate-uiview.html

编辑:此代码的作品

// Load View self.label = [[UILabel alloc] init]; self.label.text = @"TEXT"; self.label.font = [UIFont boldSystemFontOfSize:20.0]; self.label.backgroundColor = [UIColor clearColor]; [self.label sizeToFit]; [self.view addSubview:self.label]; // Animation self.label.font = [UIFont boldSystemFontOfSize:80.0]; self.label.transform = CGAffineTransformScale(self.label.transform, .25, .25); [self.label sizeToFit]; [UIView animateWithDuration:1.0 animations:^{ self.label.transform = CGAffineTransformScale(self.label.transform, 4.0, 4.0); self.label.center = self.view.center; } completion:^(BOOL finished) { self.label.font = [UIFont boldSystemFontOfSize:80.0]; self.label.transform = CGAffineTransformScale(self.label.transform, 1.0, 1.0); [self.label sizeToFit]; }]; 

你可以改变你的UILabel的大小和字体与animation像波纹pipe..在这里我只是把如何改变UILabel的字体与变换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); }]; 

我希望这对你有帮助..

我在Swift中创build了UILabel扩展。

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

如果标签文本左alignment或右alignment,则取消注释行。

您也可以使用具有fontSize作为animation属性的CATextLayer。

 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) 

我在我的项目中使用它: https : //github.com/yinanq/AngelListJobs

这个animation保持字体左上angularalignment(不同于CGAffineTransformScale从中心缩放标签),pro或con取决于您的需要。 CATextLayer的一个缺点是CALayers不能使用自动布局约束animation(我碰巧需要通过创build包含CATextLayer的UIView并为其约束设置animation来解决它)。

Swift 3.0

  UIView.animate(withDuration: 0.5, delay: 0.1, options: .curveLinear, animations: { label.transform = label.transform.scaledBy(x:4,y:4) //Change x,y to get your desired effect. } ) { (completed) in //Animation Completed } 

从2017年起…

Swift 3.0,4.0

 UIView.animate(withDuration: 0.5) { label.transform = CGAffineTransform(scaleX: 1.1, y: 1.1) //Scale label area }