Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel numberOfLines change with animation

I have a UILabel inside a UIStackView, and the stack view is inside another UIScrollView, I'm using auto layout. The label has single line (ie. numberOfLines equal to 1) and in a few cases I need to set it to multiline (ie. numberOfLines equal to 0) with an animation that expands it.

func expand() {
    label.numberOfLines = 0
}

when I click expand:

messageView.expand()
UIView.animate(withDuration: 0.3) {
    self.layoutIfNeeded()
}

However, when it expands, the label's frame isn't updated and I have to scroll (the scroll view) to make it fully visible. What could be wrong?

Thanks!

like image 915
hzxu Avatar asked Oct 12 '25 07:10

hzxu


2 Answers

Animation of UILabel doesn't perform within UIView.animate(withDuration:).
This should work:

    UIView.transition(with: label, duration: 0.5, options: .transitionCrossDissolve, animations: {
         self.label.numberOfLines = 0
    })

You can experiment with options and execution block.

like image 159
Oleg Avatar answered Oct 14 '25 02:10

Oleg


  1. Have you changed the label's text?
  2. Check if the autolayout is correct
like image 36
rocoo Avatar answered Oct 14 '25 01:10

rocoo