I have a UITextView, and I am trying to animate a change of frame when the user taps on a button. Basically the text view grows larger to fit the screen so it can display more text, and then when the user taps the button again it shrinks to its original frame.
I perform the animation using blocks, like this:
if(!isDisplayingDetailView)
{
//Expand view
[UIView animateWithDuration:0.5
animations:^{
self.detailView.frame = self.view.frame;
}
completion:^(BOOL finished){
isDisplayingDetailView = YES;
}];
}
else{
//Shrink view.
[UIView animateWithDuration:0.5
animations:^{
self.detailView.frame = self.detailFrame;
}
completion:^(BOOL finished){
isDisplayingDetailView = NO;
}];
}
Where self.detailView is the UITextView, and self.detailFrame is just a CGRect holding the original frame. isDisplayingDetailView is just a BOOL to check if the view is expanded or not. My problem is that the text resize is not animating. I made some screenshots from a test app to illustrate my problem: App default view:
The expanded view:
The textview just a moment after tapping the button:
As you can see the text automatically shrinks to the final frame size, without any kind of animation, while the bounds still are animating. I guess that's the right behavior, but I'd like to know if it's possible to animate the text along with its view.
The text in textviews doesn't always act as expected. For this you'll have to set up a NSTimer and set the frame size on every tick.
Do something like:
textview.frame = CGRectMake (textview.frame.origin.x, textview.frame.origin.y, textview.frame.size.width-1, textview.frame.size.height-1);
Then when it's done I would completely remove the textview from superview and set it to nil, and then init a new one. The reason for this is that when changes the frames of textviews for some reason padding is added around the text's box. You won't notice it unless you change the frame a probably at least 100 times.
[textview removeFromSuperview];
textview = nil;
textview = [[UITextView alloc] initWithFrame:CGRectMake(x, y, width, height)];
textview.text = yourTextString;
//You will also have to set whatever non default properties you want, such as text or background color
[view addSubview:textview];
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