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 am trying to animate it in the process. I am currently calling the following:
[UIView animateWithDuration:0.5
animations:^{
self.monthLabel.textAlignment = NSTextAlignmentLeft;
} completion:nil];
But this just jumps the label to the new alignment. Is there a way to animate this, or a better way to adjust the label to make this happen?
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];
Is your label multiline? An animation like this: http://img62.imageshack.us/img62/9693/t7hx.png?
If so, then there's a couple of alternatives.
Multiline label
Option 1:
Instead of a traditional UIView animation, try a UIView transition. The text wont slide to the left, but instead it will fade nicely to the new position.
[UIView transitionWithView:self.monthLabel
duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.monthLabel.textAlignment = NSTextAlignmentLeft;
} completion:NO];
Option 2:
You can manually work out where the new lines will appear, then create a separate UILabel for each line of text then animate the frames. This is obviously more work, but will give that desired slide animation.
Single line label
Instead of animating the textAlignment, make the label the same size of the string it contains with [self.monthLabel sizeToFit], then manually work out the framing and the centering. Then just animate the frame, the same as option 2 on a multiline label.
you can animate the FRAME, not the textAlignment.
Do a [UILabel sizeToFit] on your label if you want to get rid of any "padding" on the frame, then you can animate the frame in your animation block to move it around as you desire
CGRect frameLabel = self.monthLabel.frame;
[UIView animateWithDuration:0.5
animations:^{
frameLabel.origin.x -= 100; // e.g. move it left by 100
self.monthLabel.frame = frameLabel;
} completion:nil];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With