ios - 在 swift 2 的 textview 中允许退格

标签 ios swift textview swift2 uitextview

我想将 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/

相关文章:

ios - 在 iOS 7.1 中,UISearchBar 的文本和占位符属性不再起作用

multithreading - 检测您何时进入/离开 Xamarin.iOS 中的主线程

ios - 如何在 Swift 中单击 TextField 外部时隐藏 TextField 中的 TEXT?

swift - 对于 Swift 中的计算器

objective-c - Swift 在 Objective C 中的 "if let"等价物

android - 嵌入线性布局的 Webview 不会上下滚动,只能左右滚动

android - 更改 TextView 中一个单词的文本颜色

iOS Safari 显示已安装的应用程序。这是如何运作的?元标签?

ios - 如何管理每个tableviewCell中的AVAudioPlayer isPlaying?

Android 自定义软键盘 - 如何清除编辑文本(提交的文本,...)