0

I have set my UILabel to scale text automatically to fit within the labels frame. During runtime I am changing the size of this frame to make space for things underneath, but I really want the size change to be animated, since the text jumps from one size to a smaller one, which doesn't look very good.

Is there any way to accomplish this?

  • The guy in the question mentioned is changing the font size of his label manually, while my font size changes automatically based on the label size, using a minimum font scale. – Lucas Fray Burchardt May 12 '16 at 19:29
  • 1
    No difference. Just need to use a param. – Coder1000 May 12 '16 at 19:30
  • What? There is no code changing the font size – Lucas Fray Burchardt May 12 '16 at 19:30
  • Okay, maybe post some of your relevant code about how you change your label then. – Yuchen May 12 '16 at 19:31
  • I dont use any code to change the label size. All done with autolayout - selecting "minimum font scale" in the attribute inspector. – Lucas Fray Burchardt May 12 '16 at 19:32
  • You mentioned this: " During runtime I am changing the size of this frame to make space for things underneath". How do you change the size of the frame? – Yuchen May 12 '16 at 19:41
  • The label has constraints added. if the textview underneath is made taller, the labels height will reduce. All I am doing is changing the height of my text field – Lucas Fray Burchardt May 12 '16 at 19:42
  • 1
    okay, then you need to add animation for that -- changing the height of the text field. If you can provide a minimal reproachable example, maybe we could jump in and help. Otherwise, that's the best advice I can give now. You need to add animation to whatever code that trigger your UILabel's frame size change. If it is changed by constraints, it is okay too. Normally, the view animation will be able to calculate that properly for you. – Yuchen May 12 '16 at 20:03
  • I am already animating the height change of the textview, and the frame height of the label changes like expected. The thing I am looking for is the change in font size to be animated – Lucas Fray Burchardt May 13 '16 at 05:23

1 Answers1

0

To animate the height change use UIView.animateWithDuration. Here's an example animating the height change of a UILabel with a 2 second duration:

@IBOutlet weak var label: UILabel!

override func viewDidLoad() {
  super.viewDidLoad()

  let updatedHeight: CGFloat = 100.0
  let updatedFrame = CGRect(origin:label.frame.origin,
    size: CGSize(width:label.frame.width, height: updatedHeight))

  UIView.animateWithDuration(2.0, animations: {
    self.label.frame = updatedFrame
  })
}
  • This is not what I am looking for. I am already animating the height change l - what I am looking for is animating the automatic font size change that happens after the label has been resized, to fit the text properly. – Lucas Fray Burchardt May 13 '16 at 05:25
  • Since you are reducing the UILabels height could you use a transform instead? `let labelScale = updatedLabelFrame.height / originalLabelFrame.height` `self.label.transform = CGAffineTransformScale(self.label.transform, labelScale, labelScale)` This will give you the [following animation](http://i.stack.imgur.com/Oyk9H.gif) when inside of `UIView.animateWithDuration` –  May 13 '16 at 07:53