views:

336

answers:

3

I have a UITextView with scrolling disabled, and I am using the page-curl transition to change the text inside. This is the code I'm using:

myView.text = nextString;

[UIView transitionWithView:myView duration:PAGE_CURL_DURATION options:UIViewAnimationOptionTransitionCurlDown animations:nil completion:nil];

Everything works fine, except if the new text string has more lines than the old one, it will be clipped until the animation is complete. It looks as though the contentSize is changing AFTER the animation. I tried adding myView.contentSize = myView.bounds.size; but that didn't change anything. I also turned off "clip subviews" to no avail.

This is what it looks like immediately after the animation: alt text

a moment later the text is no longer clipped.

Any ideas how to prevent clipping?

A: 

This is rather odd. The contentSize should not clip contents, but only determine scrolling behavior. This however is the only thing changing from pre- to post-animation. And unchecking Clip Subviews always worked for me.

A few options:

I assume your UITextView.frame is large enough to contain the largest of both texts initially? If you set it just before the animation kicks in, you might be too late. In such cases tricks like [self performSelector(continueWithStuff:) withObject:object afterDelay:0.01] do wonders, because you give the UI the time needed to effectuate your changes before the animation will determine the initial state.

But the easiest fix by far will be to add a bunch of newlines to every text. Since you're not scrolling anyway this should not be a problem.

Another angle may be to embed your UITextView in a UIView which you then animate. Reading the docs, the view argument should be "The container view that contains the views involved in the transition." Ignoring this fact may be giving your surprising results.

mvds
For the record: UIViewAnimationOptionAllowAnimatedContent allowed the UITextView to update while animating, stopping the clipping issue.
Kenny Winker
A: 

The easiest way to fix you problem is to set the UITextView's property "clipsToBounds" to NO. This will make your text view available to draw outside of its bounds

Bjarne Mogstad
It wasn't a problem of the view clipping to bounds, instead it was just not updating during the animation. UIViewAnimationOptionAllowAnimatedContent fixed it.
Kenny Winker
+1  A: 

The easy way is to do what Bjarne said - use the clipsToBounds property. But watch out - this will make the text expand downwards indefinitely. So you will also need to surround your text field with a container view that DOES clip to bounds to set an upper limit.

If that doesn't do the trick, you will have to do some work to manually expand your text view's bounds and reposition it before animating if the new text is larger.

To get the size of a text, check out UIStringDrawing.h in the UIKit framework. Specifically, I'm thinking of using: - (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode; together with UILineBreakModeWordWrap.

another alternative to getting the size may be this function in UILabel: - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines; and passing in the bounds of the largest possible text view.

For extra credit, you may also want to consider animating the change in your view's bounds for a smoother transition.

Rolf Hendriks
this seemed like it was going to fix it, but it didn't.UIViewAnimationOptionAllowAnimatedContent did.Still good advice! Thanks for the tips!
Kenny Winker