I have a UILabel that's initiated with 3 lines of text:
locationDescription = [[UILabel alloc]init];
locationDescription.numberOfLines = 3;
[locationDescription setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addSubview:locationDescription];
I then have a button that expands the label:
- (void)buttonPressed:(UIButton *)sender{
NSLog(@"Pressed");
[UIView animateWithDuration:1
animations:^{locationDescription.numberOfLines = 0;}
];
}
The desired behavior for the label is to have each extra line reveal itself one at a time. The label expands fine, but the transition isn't animated and all the lines just show up at once.
What am I missing?
You can animate number of lines. It changes the intrinsicSize of the UILabel
. You need to call layoutIfNeeded inside your animation block on the view containing the UILabel. Here I attach a tap gesture to the table to toggle it between having 0 (as many as you want) lines and 1 line.
func tapLabel(tapGesture: UITapGestureRecognizer)
{
if let label = tapGesture.view as? UILabel {
label.numberOfLines = label.numberOfLines == 0 ? 1 : 0
UIView.animateWithDuration(0.5) {
label.superview?.layoutIfNeeded()
}
}
}