当字符串粘贴到uitextview中时,会在文本底部添加额外的空格

时间:2018-06-19 06:20:10

标签: ios uitextview swift4 uitextviewdelegate

当我粘贴UITextView中的文本时,会添加一些额外的空间。但是当我滚动它时,textView根据内容拟合。在textViewDidChange中,textView高度约束根据其内容大小进行设置。有人遇到同样的问题吗?

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

1 个答案:

答案 0 :(得分:2)

您必须将textView的smartInsertDeleteType设置为.no

textView.smartInsertDeleteType = .no

参考链接:https://developer.apple.com/documentation/uikit/uitextinputtraits/2865828-smartinsertdeletetype