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)?

#11
Here's how you could do it in Swift 3/4.

First, make sure you've got your textField's delegate set in `viewDidLoad`.

yourTextField.delegate = self

Then, implement `shouldChangeCharactersIn`:


extension YourViewController: UITextFieldDelegate {
func textField(_ textFieldToChange: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// limit to 4 characters
let characterCountLimit = 4

// We need to figure out how many characters would be in the string after the change happens
let startingLength = textFieldToChange.text?.characters.count ?? 0
let lengthToAdd = string.characters.count
let lengthToReplace = range.length

let newLength = startingLength + lengthToAdd - lengthToReplace

return newLength <= characterCountLimit
}
}

Additional details [here](

[To see links please register here]

)
Reply

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

Reply

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

![enter image description here][1]


[1]:
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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