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?

NSPratik
  • 4,714
  • 7
  • 51
  • 81
Roduck Nickes
  • 1,021
  • 2
  • 15
  • 41

3 Answers3

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
    }
}
Okhan Okbay
  • 1,374
  • 12
  • 26
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • @Learn2Code- use this [SwiftUI UITextView Coordinator not working](https://stackoverflow.com/questions/57597060/swiftui-uitextview-coordinator-not-working) – Anbu.Karthik May 01 '20 at 06:23
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
    }

}
Ahmed Safadi
  • 4,402
  • 37
  • 33
  • Will, I didn't get deep on it, check this https://stackoverflow.com/questions/61535314/swiftui-display-characters-count-remaining-in-a-label-text – Ahmed Safadi May 01 '20 at 12:26
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)"
}