For some reason, UILabel's text wants to set its alignment without animation, and I cannot figure out how to get the text to animate along with the rest of the label.
I have the following code right now:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40,200,100,50)];
[label setText:@"Label Name"];
[label setBackgroundColor:[UIColor blueColor]];
[label setFont:[UIFont systemFontOfSize:17.0]];
[label setTextColor:[UIColor blackColor]];
[label setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:label];
[UIView animateWithDuration:3 animations:^{
[label setFrame:CGRectMake(40,200,200,300)];
}];
This code is called in a button's action method. Normally the animation will happen separately from the creation of the label but I put it in the same method to test it.
For some reason when this runs, the label gets resized perfectly as it is supposed to and I can see the frame resize. However, the text aligns itself in the horizontal axis to the position that it will be at at the end of the animation. In the vertical axis, however, it animates properly.
Right now this is what the animation looks like:
For some reason, the label's text stays centered when moving up and down but it does not stay centered left-right, as I would assume it should.
Ive looked all over and couldn't find a solution to this. Does anyone know what UILabels behave like this and if there is any way around it?
You could add an UIView and then add UILabel as its subview: