Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize UITextView while typing inside it?

I'm creating a comment section much like the one Facebook uses for it's messaging section in it's iOS app. I want the UITextView to resize the height so the text I'm typing fits inside it rather than you having to scroll to see the text that overflows. Any ideas how I might go about doing this? I've looked into maybe using a CGRect that is assigned to the size and height of the text view which then matches the content size:

CGRect textFrame = textView.frame;
textFrame.size.height = textView.contentSize.height;
textView.frame = textFrame;

I assume I need some sort of function that detects when the text reaches the bounds of the UITextView and then resizes the height of the view? Has anyone struggled with this same concept?

like image 727
Ollie177 Avatar asked Nov 21 '12 12:11

Ollie177


2 Answers

You can adjust frame in this delegate method, do not forget to set textView's delegate to self.

-(BOOL)textView:(UITextView *)_textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
      [self adjustFrames];  
      return YES;
}


-(void) adjustFrames
{
   CGRect textFrame = textView.frame;
   textFrame.size.height = textView.contentSize.height;
   textView.frame = textFrame;
}

this solution is for iOS6 and prior... for iOS7 refer to this

StackOverflow Answer

like image 65
waheeda Avatar answered Oct 16 '22 23:10

waheeda


This is my solution, using autolayout and textView.contentSize.height. Tested on iOS8 Xcode6.3 beta4.

There's one catch about the setContentOffset at the end. I put it to avoid "wrong contentOffset" artefact when line count changes. It adds an extra unwanted blank space below the last line and it doesn't look very nice unless you set it back right after changing the constraint. Took me hours to figure this out!

// set this up somewhere
let minTextViewHeight: CGFloat = 32
let maxTextViewHeight: CGFloat = 64

func textViewDidChange(textView: UITextView) {

    var height = ceil(textView.contentSize.height) // ceil to avoid decimal

    if (height < minTextViewHeight + 5) { // min cap, + 5 to avoid tiny height difference at min height
        height = minTextViewHeight
    }
    if (height > maxTextViewHeight) { // max cap
        height = maxTextViewHeight
    }

    if height != textViewHeight.constant { // set when height changed
        textViewHeight.constant = height // change the value of NSLayoutConstraint
        textView.setContentOffset(CGPointZero, animated: false) // scroll to top to avoid "wrong contentOffset" artefact when line count changes
    }
}
like image 35
Hlung Avatar answered Oct 17 '22 00:10

Hlung