Change size of label's frame with animation

2019-06-11 09:22发布

问题:

I would like to know if there is a way to change a label's frame's width (not the fontSize or something) with a smooth animation.

I already tried the following, that did (obviously) not work:

_myLabel.frame = CGRectMake(139,193,42,21);

[UIView animateWithDuration:1 animations:^ {
    _myLabel.frame = CGRectMake(139, 193, 100, 21);
} completion:^(BOOL finished) {
}];

So what basically happened after running this code was that it changed the width as expected but without any animation.

Any ideas?

回答1:

You can't change the frame size of labels... You can reach correct animation behavior.. set label.contentMode = UIViewContentModeCenter but it looks ugly

But you can...:

  • change is the bounds, rather than the frame https://stackoverflow.com/a/3304220
  • add an UIView and then add UILabel as its subview and animate UIView https://stackoverflow.com/a/15639093 and https://stackoverflow.com/a/22224630
  • Use a CGAffineTransform to do this but have some ugly side effects like distorting the text on the label https://stackoverflow.com/a/13066366
  • change the font of UILabel with transform Animation Animating UILabel Font Size Change