How to resize UITextView while typing inside it?

こ雲淡風輕ζ 提交于 2019-12-02 23:17:59
waheeda

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

Hlung

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
    }
}
Bart Herrijgers

On the TableViewController that holds the UITextView, update the data from the tableViewDataSource that gets put in the cell and then simply call this:

tableView.beginUpdates()
tableView.endUpdates()

Unlike tableView.reloadData(), this does not call resignFirstResponder

First set the minimum height constraints to your TextView:

textView.heightAnchor.constraint(greaterThanOrEqualTo: view.heightAnchor, multiplier: 0.20)

(Make sure you are setting greaterThanOrEqualTo Constraint so that if intrinsic content height is more than this height, it takes intrinsic content height )

OR simple constant

textView.heightAnchor.constraint(greaterThanOrEqualToConstant: someConstant)

While configuring your textView, set isScrollEnabled to false

textView.isScrollEnabled = false

Now when you type on the textView, its intrinsic content size height will increase and it will push views below it automatically.

gunas

contentsize will not work in ios 7. Try this:

CGFloat textViewContentHeight = textView.contentSize.height;

 textViewContentHeight = ceilf([textView sizeThatFits:textView.frame.size].height + 9);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!