简体   繁体   中英

Extra space is added at the bottom of text in uitextview when string is pasted into it

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

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

textView.smartInsertDeleteType = .no

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM