When I\'ve tried How to you set the maximum number of characters that can be entered into a UITextField using swift?, I saw that if I use all 10 characters, I can\'t erase t
You can use in swift 5 or swift 4 like image look like bellow
add the code in view ViewController
class ViewController: UIViewController , UITextFieldDelegate {
@IBOutlet weak var txtName: UITextField!
var maxLen:Int = 8;
override func viewDidLoad() {
super.viewDidLoad()
txtName.delegate = self
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if(textField == txtName){
let currentText = textField.text! + string
return currentText.count <= maxLen
}
return true;
}
}
You can download Full Source form GitHub: https://github.com/enamul95/TextFieldMaxLen