Is there a way to animate changing a UILabel's textAlignment?

后端 未结 5 563
傲寒
傲寒 2020-12-06 11:25

I am using iOS 7 and I am trying to move a label that is centered off to the left of my view. Currently I am trying to do this by changing how my UILabel is aligned, and I a

5条回答
  •  爱一瞬间的悲伤
    2020-12-06 12:03

    Set the UILabel frame size to exactly contain the text and center the UILabel in your view.

    self.monthLabel.text = @"February";
    [self.monthLabel sizeToFit];
    self.monthLabel.center = parentView.center; // You may need to adjust the y position
    

    Then set the alignment which should not affect the layout since there will be no extra space.

    self.monthLabel.textAlignment = NSTextAlignmentLeft;
    

    Next, animate the UILabel frame size so it slides over where you want it.

    [UIView animateWithDuration:0.5
                 animations:^{
                      CGRect frame = self.monthLabel.frame;
                      frame.origin.x = 10;
                      self.monthLabel.frame = frame;
                 } completion:nil];
    

提交回复
热议问题