[swift]剩余的 UITextView 字符

标签: UITextView ios Swift
发布时间: 2016/5/8 19:41:16
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

如何可以设置上的剩余字符 UILabelUITextView

我这样做了 UITextField ,但相同的代码不能工作。

这是我过 ︰

func textView(textView: UITextView, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
 {
        if string == ""
        {
            if plainTextView.text!.characters.count == 0
            {
                charCount = 0
                countLabel.text = String(format: "%i Characters Left", maxLength - charCount)
                return false
            }
            charCount = (plainTextView.text!.characters.count - 1)
            countLabel.text = String(format: "%i Characters Left", maxLength - charCount)
            return true
        }
        else
        {
            charCount = (plainTextView.text!.characters.count + 1)
            countLabel.text = String(format: "%i Characters Left", maxLength - charCount)

            if charCount >= maxLength + 1
            {
                charCount = maxLength
                countLabel.text = String(format: "%i Characters Left", maxLength - charCount)
                return false;
            }
        }
        return true
    }

有何建议?

解决方法 1:

试试这个

func textViewDidChange(textView: UITextView) {
    countLabel.text = "\(200 - textView.text.characters.count)"
}


 func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
return textView.text.characters.count + (text.characters.count - range.length) <= 200
}
官方微信
官方QQ群
31647020