Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 532 Vote(s) - 3.4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to do a live UITextField count while typing (Swift)?

#1
I would like to count the character when user keep typing in **UITextField** with swift.

Image of Field and Label:

![enter image description here][1]

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.


[1]:
Reply

#2
func updateCharacterCount() {
let count = DescriptionTextview.text.count
descountlabel.text = "\((0) + count)/140"
}

extension DescriptionCollectionViewCell: UITextViewDelegate{

func textViewDidChange(_ textView: UITextView) {
descountlabel.becomeFirstResponder()
self.updateCharacterCount()
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool{
let generator = UINotificationFeedbackGenerator()
let count = textView.text.count + (text.count - range.length)
if count <= 140 {
return true
}else{
generator.notificationOccurred(.error)
return false
}
}

}
Reply

#3
Simple...

Add a `UITextViewDelegate` to your class header like this...

class ViewController: UIViewController, UITextFieldDelegate { ... }

Add the `@IBOutlet` to both the `UILabel` and the `UITextView` like so...

@IBOutlet weak var characterCountLabel: UILabel!
@IBOutlet var characterTextField: UITextView!

Then add the following delegate to the `viewDidLoad()` function...

override func viewDidLoad() {
super.viewDidLoad()
self.characterTextField.delegate = self // required for character counter
}

Finally, just create a `func` for the `textViewDidChange` delegate method...

func textViewDidChange(_ textView: UITextView) {
self.characterCountLabel.text = String("\(textView.text.count)")
}

I personally checked and verified this simple way to add a text counter for your `UITextView` and output it to a `UILabel` and it works!
Reply

#4
Swift 5.3

Add **UITextFieldDelegate** in you viewController with outlets

class ViewController: UIViewController, UITextFieldDelegate{
@IBOutlet weak var txtGroupName: UITextField!
@IBOutlet weak var lblWordCount: UILabel!
Add **textfeild delegate** in your **viewDidLoad()** like below

override func viewDidLoad() {
super.viewDidLoad()
txtGroupName.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: UIControl.Event.editingChanged)
}

@objc func textFieldDidChange(textField : UITextField){
self.lblWordCount.text = "\(self.txtGroupName.text!.count)/"+"\(65)"
}

We have set also **meximum text limit 65** . you can **replace 65 to what you want**.

extension viewController:UITextFieldDelegate{
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if !(textField.text?.isEmpty ?? true){
return textField.text!.count + (string.count - range.length) <= 65
}
return true
}
}

It's **output** is like below

[![output][1]][1]


[1]:
Reply

#5
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
}



Reply

#6
`UITextView` count while typing in Swift:

```
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

let textCount = textView.text?.count ?? 0 + text.count - range.length
self.textCountLabel.text = "\(textCount)"
return true
}
```
Reply

#7
Swift 5.1

Add `viewController` delegate

final class CreditCardViewController : BaseViewController , UITextFieldDelegate {

@IBOutlet var cvvTextField: UITextField!


Add `viewDidLoad()`

cvvTextField.delegate = self
And add delegate function.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if(textField == cvvTextField){
let characterCountLimit = 3
let startingLength = textField.text?.count ?? 0
let lengthToAdd = string.count
let lengthToReplace = range.length

let newLength = startingLength + lengthToAdd - lengthToReplace

return newLength <= characterCountLimit
}

return true
}
Reply

#8
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

<!-- language: lang-swift -->

class ViewController: UIViewController, UITextFieldDelegate

You should have an `@IBOutlet` similar to this

<!-- language: lang-swift -->

@IBOutlet var txtValue: UITextField

Set the `UITextField` s delegate to `self`.

<!-- language: lang-swift -->

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.
Reply

#9
Swift 4 for `UITextView` change text event `UITextViewDelegate`

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

}
Reply

#10
For multiple `UITextView`

[![enter image description here][1]][1]

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



[1]:
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through