0

When i am pasting the text in UITextView then some extra space is getting added into it. But when i scroll it then the textView is fitted according to the content. In textViewDidChange, textView height constraint is being set according to its content size. Anybody faced the same issue?

func textViewDidChange(_ textView: UITextView) {
    let newSize = textView.sizeThatFits(CGSize.init(width: textView.frame.size.width, height: CGFloat(MAXFLOAT)))
    textView.isScrollEnabled = false
    if newSize.height < 37 {
        self.messageTextViewHeightConstraint.constant = minimumTextviewHeight
    }
    if (newSize.height >= 37 && newSize.height <= 100) {
        self.messageTextViewHeightConstraint.constant = newSize.height
    }
    if (newSize.height >= 100) {
        self.messageTextViewHeightConstraint.constant = maximumTextviewHeight
        self.textView.isScrollEnabled = true
    }
}
cherry_4
  • 158
  • 2
  • 17
  • @s-v : TextView when scroll disabled gets intrinsic size from content set, if you are setting height constraint manually u might be affecting the content size of textView. So show us the code where u set the constraint – Sandeep Bhandari Jun 19 '18 at 06:31
  • please check my updated question – cherry_4 Jun 19 '18 at 08:47

1 Answers1

3

You have to set the textView's smartInsertDeleteType to .no

textView.smartInsertDeleteType = .no

Reference link: https://developer.apple.com/documentation/uikit/uitextinputtraits/2865828-smartinsertdeletetype

Jonathan King
  • 31
  • 1
  • 4