【问题标题】:UITextView characters remainingUITextView 剩余字符
【发布时间】:2016-05-10 21:41:04
【问题描述】:

如何为UITextView 设置UILabel 上剩余的字符?

我已经为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
    }

有什么建议吗?

【问题讨论】:

  • 请提供您尝试过的代码
  • @user4261201 - 更新问题。
  • 你的最大字符数限制是多少..
  • @Anbu.Karthik 最大字符数限制为 200。

标签: ios swift uitextview uitextviewdelegate


【解决方案1】:

试试这个

import UIKit

class ViewController: UITextViewDelegate {
    let maxLenghth = 200
    
    func textViewDidChange(_ textView: UITextView) {
        countLabel.text = "\(maxLength - textView.text.count)"
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        return textView.text.count + (text.count - range.length) <= maxLength
    }
}

【讨论】:

【解决方案2】:
func textViewDidBeginEditing(_ textView: UITextView) {
    shrinkText()
}

func textViewDidChange(_ textView: UITextView) {
    shrinkText()
}

func textViewDidEndEditing(_ textView: UITextView) {
    shrinkText()
}


func shrinkText() {
    sendButton.isEnabled = reviewTextField.text == "" ? false : true
    
    var text = ""
    var counter = 0
    
    if reviewTextField.text.count > limit {
        reviewTextField.text.forEach {
            if counter < limit {
                counter += 1
                text.append($0)
            } else {
                return
            }
        }
        reviewTextField.text = text
    }
    counterLabel.text = "\(reviewTextField.text.count) / \(limit)"
}

【讨论】:

    【解决方案3】:

    斯威夫特 5

    extension AddProperty2ViewController: UITextViewDelegate {
    
        func textViewDidChange(_ textView: UITextView) {
            p_summary_line_textview.text = "\(5000 - textView.text.count)"
        }
    
    
        func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
            return textView.text.count + (text.count - range.length) <= 5000
        }
    
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    相关资源
    最近更新 更多