How to do a live UITextField count while typing (Swift)?
Solution 1
To use the function below you need to implement the UITextFieldDelegate protocol
on the text field you want to count. This gets called every time the UITextField
s text changes:
Your class declaration should look something like this
class ViewController: UIViewController, UITextFieldDelegate
You should have an @IBOutlet
similar to this
@IBOutlet var txtValue: UITextField
Set the UITextField
s delegate to self
.
override func viewDidLoad() {
super.viewDidLoad()
txtValue.delegate = self
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let newLength = count(textField.text.utf16) + count(string.utf16) - range.length
mylabel.text = String(newLength) // Set value of the label
// myCounter = newLength // Optional: Save this value
// return newLength <= 25 // Optional: Set limits on input.
return true
}
Note that this function is called on all UITextField
s so if you have several UITextField
s you will need to add a logic to know witch one is calling this function.
Solution 2
A very elegant and neat solution exists using UITextFieldDelegate. My solution uses the concept of selectors. In a selector you tell your compiler what function/action to perform when a particular event happens. In this case - typing in textfield. Make sure that you have set the textField delegate in storyboard.
override func viewDidLoad() {
super.viewDidLoad()
yourTextField.addTarget(self, action: #selector(YourViewController.textFieldDidChange(_:)), for: UIControlEvents.EditingChanged)
}
@objc func textFieldDidChange(textField : UITextField){
label.text = yourTextField.text?.count
}
Solution 3
For multiple UITextView
class ViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var ticketSummaryTV: UITextView!
@IBOutlet weak var ticketDescriptionTV: UITextView!
@IBOutlet weak var summaryCountLbl: UILabel!
@IBOutlet weak var descriptionCountLbl: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
ticketSummaryTV.delegate = self
ticketDescriptionTV.delegate = self
self.updateCharacterCount()
}
func updateCharacterCount() {
let summaryCount = self.ticketSummaryTV.text.characters.count
let descriptionCount = self.ticketDescriptionTV.text.characters.count
self.summaryCountLbl.text = "\((0) + summaryCount)/50"
self.descriptionCountLbl.text = "\((0) + descriptionCount)/500"
}
func textViewDidChange(_ textView: UITextView) {
self.updateCharacterCount()
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool{
if(textView == ticketSummaryTV){
return textView.text.characters.count + (text.characters.count - range.length) <= 50
}else if(textView == ticketDescriptionTV){
return textView.text.characters.count + (text.characters.count - range.length) <= 500
}
return false
}
}
Solution 4
This will only allow your textfield input 14 char
class ViewController: UIViewController,UITextFieldDelegate {
@IBOutlet weak var textfield: UITextField!
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.label.text = "14"
self.textfield.delegate = self
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let newLength = count(textField.text.utf16) + count(string.utf16) - range.length
if(newLength <= 14){
self.label.text = "\(14 - newLength)"
return true
}else{
return false
}
}
}
And Screenshot
Solution 5
In Swift
viewDidLoad {
self.yourTextView.delegate = self
self.updateCharacterCount()
}
func updateCharacterCount() {
self.yourLabel.text = "\((65) - self.yourTextView.text.characters.count)"
}
func textViewDidChange(textView: UITextView) {
self.updateCharacterCount()
}
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
self.updateCharacterCount()
return textView.text.characters.count + (text.characters.count - range.length) <= 65
}
Related videos on Youtube
Brian Nezhad
Innovative, driven, software engineer highly regarded for creating and delivering on varies of platforms. Demonstrated desire to learn and share technological knowledge. An out-of-the-box thinker who excels in fast-paced, collaborative environments. Consistency Is The Key.
Updated on July 09, 2022Comments
-
Brian Nezhad almost 2 years
I would like to count the character when user keep typing in UITextField with swift.
Image of Field and Label:
I have already placed UITextField and UILabel, just haven't found any information on Stack overflow, also if you can do one in UITextView I also appreciate it.
-
Brian Nezhad almost 9 yearsHow to connect it to label? sorry I'm kind of beginner
-
Brian Nezhad almost 9 yearsOkay I got it, but how to change the small label on bottom of my text field?
-
Icaro almost 9 yearsSorry, I have no idea what label you are referring to.
-
Brian Nezhad almost 9 yearsWhere is "142" in the picture I attached to this question.
-
Icaro almost 9 yearsI also add to the answer supposing the label is call mylabel and you have an IBOutlet to it
-
Brian Nezhad almost 9 yearsThank you, Works! I will try to figure out UItextview,
-
Awesome-o almost 4 yearsthis doesn't seem to get called when pressing the (x) clear search button unfortunately