我想将 TextView 限制为 50 个字符。我做到了,但在 50 个字符后我无法按退格键。如何解决问题?我的代码如下(注意:“txtv”是我的 TextView 名称)
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if (self.txtv.text.characters.count) >= 50 {
return false
}
return true
}
最佳答案
如果用户剪切文本或删除长于单个字符的字符串(即,如果他们选择然后按退格键),请这样做
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
return textView.text.characters.count + (text.characters.count - range.length) <= 50
}
关于ios - 在 swift 2 的 textview 中允许退格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35125329/