UITextView animated frame change does not revive text redistribution

I have a UITextView and am trying to animate a frame change when the user clicks a button. Basically, the text view becomes larger to fit the screen so that it can display more text, and then when the user clicks the button again, it shrinks to the original frame.

I am doing animation using blocks, for example:

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 a UITextView and self.detailFrame is just a CGRect containing the original frame. isDisplayingDetailView is just a BOOL to check if the view is expanding. My problem is that resizing text doesn’t make things live. I took some screenshots from a test application to illustrate my problem: Default appearance of the application: Default view

Extended view:

Expanded view

Text image an instant after pressing a button:

Shrinking view

As you can see, the text is automatically compressed to the final frame size without any animation, while the borders are still animated. I assume that the correct behavior, but I would like to know whether it is possible to animate the text along with its presentation.

+6
source share
2 answers

Text in text views does not always work as expected. To do this, you will need to configure NSTimer and set the frame size on each 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 this is done, I will completely remove the text view from the supervisor and set it to zero, and then start a new one. The reason for this is that when you change the frames of text views for some reason, the addition is added around the text field. You will not notice it unless you change the frame 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]; 
+2
source

Another solution is to revitalize border changes.

There are several ways, but there is a simple subclass of UITextView that does just that.

 import Foundation import UIKit import QuartzCore class SmoothTextView : UITextView { override func actionForLayer(layer: CALayer!, forKey key: String!) -> CAAction! { if key == "bounds" { let x = super.actionForLayer(layer, forKey: "backgroundColor") if let action:CAAnimation = x as? CAAnimation { let transition = CATransition() transition.type = kCATransitionFade transition.beginTime = action.beginTime transition.duration = action.duration transition.speed = action.speed transition.timeOffset = action.timeOffset transition.repeatCount = action.repeatCount transition.repeatDuration = action.repeatDuration transition.autoreverses = action.autoreverses transition.fillMode = action.fillMode transition.timingFunction = action.timingFunction transition.delegate = action.delegate return transition } } return super.actionForLayer(layer, forKey: key) } } 

This is with iOS 8.

As an additional setting, you can customize the text of your text view instance by setting up a text container, zeroing out all additions or inserts:

 textView.textContainer.lineFragmentPadding = 0.0 textView.textContainerInset = UIEdgeInsetsZero 
+1
source

All Articles