I've been looking for a solution in the site to dismiss keyboard after a user entered 11 digits. I found a Solution here, but it is an Objective-c and I'm looking for a swift code, can anybody help?
Asked
Active
Viewed 1,289 times
-6
-
1First of all, show us what you tried ? – Ratul Sharker Jul 17 '18 at 06:39
-
1Converting from Objective-C is a really important, real world skill - so much code is still/only available in ObjC, worth taking the time to try and convert it – MadProgrammer Jul 17 '18 at 06:41
-
1You forgot to try by your self or put it here – Prashant Tukadiya Jul 17 '18 at 06:42
-
@MadProgrammer I don't want to convert it !! I am looking for the swift one! – Am1rFT Jul 17 '18 at 06:43
-
@Am1rFT :/ Then I recommend that you keep looking - it's undoubtful that anyone is going to write one for you - that's not what SO is for - no offence – MadProgrammer Jul 17 '18 at 06:44
-
I have an If statement : if National_ID.text?.count == 11{ ... } like this . when a button is pressed ,and this if statement return true , then app will continue , but it won't prevent the user not to enter more than 11 digits ! – Am1rFT Jul 17 '18 at 06:45
-
share you objc code which you wants to convert. – Shamim Hossain Jul 17 '18 at 06:49
-
@ShamimHossain it is available in the question – Am1rFT Jul 17 '18 at 06:50
-
use this @IBAction func tfEdiotingChange(_ sender: UITextField) { print("tfEdiotingChange...\(String(describing: sender.text))") if sender.text?.count == 11 { sender.resignFirstResponder() } } this IBAction is the editingchanged outlet of the text field. – vivekDas Jul 17 '18 at 06:55
-
textField.addTarget(self, action: #selector(textChanged), for: . editingChanged) func textChanged() { let count: Int = textField.text?.count ?? 0 if count >= 11 { textField.resignFirstResponder() } } – DHEERAJ Jul 17 '18 at 06:59
2 Answers
2
This is how it works
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// YOU SHOULD FIRST CHECK FOR THE BACKSPACE. IF BACKSPACE IS PRESSED ALLOW IT
if string == "" {
return true
}
if let characterCount = textField.text?.count {
// CHECK FOR CHARACTER COUNT IN TEXT FIELD
if characterCount >= 11 {
// RESIGN FIRST RERSPONDER TO HIDE KEYBOARD
return textField.resignFirstResponder()
}
}
return true
}
EDIT
1) You should set the IBOutlet to your textField
2) Set delegate to self, on your textField.
3) YourViewController: UIViewController, UITextFieldDelegate { }
4) Implement the delegate method as in above. check for the backspace and allow if user enters backspace to remove characters from textField.

Bhavin Kansagara
- 2,866
- 1
- 16
- 20
-
why should we need " shouldChangeCharactersIn range: NSRange, replacementString string: String " – Am1rFT Jul 17 '18 at 06:49
-
this is the delegate method, which checks wether to allow user to type the next character or not. so, when the count of the characters in textField goes above 11, keyboard resigns – Bhavin Kansagara Jul 17 '18 at 06:52
-
this is the delegate method, which checks wether to allow user to type the next character or not. so, when the count of the characters in textField goes above 11, keyboard resigns – Bhavin Kansagara Jul 17 '18 at 06:52
-
-
You won't have to call this function, it is a delegate method for UITextField, You will have to implement the above delegate method and it will automatically check for conditions and do the needful. – Bhavin Kansagara Jul 17 '18 at 06:58
-
it works, but it has a minor bug after it reaches the limit! after it dismisses the keyboard, it doesn't let me delete the text/character!! – Am1rFT Jul 17 '18 at 07:10
-
-
-1
you may try this :
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let count: Int = textField.text?.count ?? 0
if count >= 11 {
textField.resignFirstResponder()
}
return true
}

Rizwan
- 3,324
- 3
- 17
- 38
-
it won't work, because while the delegate will be called the textField won't contain the total change. This delegate is asking the developer that, will the developer change the previous state of the `UITextField` given the next state. – Ratul Sharker Jul 17 '18 at 06:48
-
But the code that you have written below is very much same as that one I replied with – Rizwan Jul 17 '18 at 06:51
-
check carefully i have not written any answer. As the other answers are same, it simply implies that won't work too, because code don't lies :) – Ratul Sharker Jul 17 '18 at 06:52
-
sorry you have not written that somebody else. But still what is the problem in this code, it should work as expected, after 10th char as soon as 11th is typed it will hide the keyboard – Rizwan Jul 17 '18 at 06:54