0

I am using the following code to dynamically adjust the height of the containerView which contains my UITextView

    func textViewDidChange(textView: UITextView) {
    let amountOfLinesToBeShown:CGFloat = 6
    let maxHeight:CGFloat = textView.font!.lineHeight * amountOfLinesToBeShown

    if ((textView.contentSize.height / textView.font!.lineHeight) < 6) {
        topConstraint?.constant = -textView.sizeThatFits(CGSizeMake(textView.frame.size.width, maxHeight)).height - keyboardFrameSize!.height

        containerView.layoutIfNeeded()
        containerView.updateConstraints()
    }
}

The problem is, when I am typing, it appears like this: enter image description here

and the desired effect is this:

enter image description here

Alk
  • 5,215
  • 8
  • 47
  • 116

4 Answers4

1

The top answer from this post solves this issue. How do I size a UITextView to its content?

The code below is not mine, it is from the answer linked above (simply posting for other's convenience):

let fixedWidth = textView.frame.size.width
textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height:CGFloat.max))
var newFrame = textView.frame
newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
textView.frame = newFrame;
Community
  • 1
  • 1
Alk
  • 5,215
  • 8
  • 47
  • 116
1

The code below might be the solution for this problem you got.

   func textViewDidChange(textView: UITextView) {
        var newFrame = textView.frame
        var minHeight = 40 // ENTER THE MIN HEIGHT OR THE INITIAL HEIGHT OF THE TEXTVIEW
        var maxHeight = 100 // ENTER THE MAX HEIGHT YOU WANT
        newFrame.size  = textView.sizeThatFits(CGSizeMake(newFrame.size.width, maxHeight)) 

    if newFrame.size.height > minHeight && newFrame.size.height < maxHeight {
        // Change the origin.y value which is 10 to your desired margin for top.
        textView.frame = CGRectMake(0, 10, textView.frame.size.width, newFrame.size.height)
    }
}
sarah
  • 580
  • 1
  • 6
  • 24
Ayazmon
  • 1,360
  • 8
  • 17
0

Create a function and add this in your viewDidLoad method, try with:

self.addSubview(containerView)
containerView.addSubview(textView)

containerView.bottomAnchor.constraintEqualToAnchor(self.bottomAnchor).active=true
containerView.leftAnchor.constraintEqualToAnchor(self.leftAnchor).active=true

textView.topAnchor.constraintEqualToAnchor(containerView.topAnchor, constant: 3).active=true
textView.leftAnchor.constraintEqualToAnchor(containerView.leftAnchor, constant: 3).active=true
textView.widthAnchor.constraintEqualToAnchor(containerView.widthAnchor).active=true
textView.heightAnchor.constraintEqualToAnchor(containerView.heightAnchor).active=true
Carlo
  • 813
  • 1
  • 15
  • 34
  • Should I do this in addition to my code or instead of my code? Keep in mind that the `UITextView` height should dynamically change when I type, wouldn't setting the height anchor to 30 prevent that? – Alk Sep 03 '16 at 18:23
  • You can put heightAnchor as containerView.heightAnchor. – Carlo Sep 03 '16 at 18:24
  • behaves the same as before – Alk Sep 03 '16 at 18:30
  • If number of Lines is greater than 2 Lines, the heightAnchor container view is double. – Carlo Sep 03 '16 at 20:44
0

use textview textContainerInset method for achieving what u want

//insets are top, left, bottom, top..
tv.textContainerInset = UIEdgeInsetsMake(0,0,0,0);

after setting this method,when textview line starts, increase height of textview for finding the textview next line start,

use method:- TextViewDidChange

Yasin Patel
  • 5,624
  • 8
  • 31
  • 53
avish manocha
  • 31
  • 1
  • 2