2

How can I set characters remaining on a UILabel for the UITextView?

I have done this for the UITextField, but the same code does not work..

This is what I have tried:

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
    }

Any suggestions?

4
  • Please provide the code which you have tried Feb 2, 2016 at 7:30
  • @user4261201 - Updated question. Feb 2, 2016 at 7:31
  • whats your max chars limit .. Feb 2, 2016 at 7:35
  • @Anbu.Karthik The max character limit is 200. Feb 2, 2016 at 7:37

3 Answers 3

12

try this

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

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

}
1
0
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)"
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.