How do I fail the space bar on a keyboard in Swift?

I am creating a login system and I do not want spaces to be allowed in the username field. Instead of validating and validating the field, I want to prohibit adding spaces.

So, every time the user presses the space bar, nothing should happen. How can i do this? I saw Instagram doing this.

This is my code:

import UIKit class ViewController: UIViewController, UITextFieldDelegate { @IBOutlet weak var signUp: UIBarButtonItem! @IBOutlet weak var navBar: UINavigationBar! @IBOutlet weak var UsernameText: UITextField! override func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBar.clipsToBounds = true UsernameText.attributedPlaceholder = NSAttributedString(string:"TypeYourUsername", attributes:[NSForegroundColorAttributeName: UIColor.whiteColor()]) func textField(UsernameText: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { if (string == " ") { return false } return true } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func viewWillAppear(animated: Bool) { UsernameText.becomeFirstResponder() } override func prefersStatusBarHidden() -> Bool { return true } } 
+7
source share
6 answers

Yes you can do it. Overriding the UITextField delegate method shouldChangeCharactersInRange . And in this method, check for a space character. If found, than return false.

Note. Remember to specify a delegate for the text box.

 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { if (string == " ") { return false } return true } 

Edit: More code

 class ViewController : UIViewController, UITextFieldDelegate { @IBOutlet weak var textField : UITextField! override func viewDidLoad() { super.viewDidLoad() textField.delegate = self } func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { if (string == " ") { return false } return true } } 

Here the textField object is an IBOutlet. This means that text field management is in the storyboard and is associated with this variable.

+13
source

In Swift 2.0

 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { let whitespaceSet = NSCharacterSet.whitespaceCharacterSet() let range = string.rangeOfCharacterFromSet(whitespaceSet) if let _ = range { return false } else { return true } } 
+6
source

Swift 4.1 / 4.2 Xcode 10.1

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { if string == " " { return false }else{ return true } } 
+5
source

In Swift 2.1

 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { let whitespaceSet = NSCharacterSet.whitespaceCharacterSet() if let _ = string.rangeOfCharacterFromSet(whitespaceSet) { return false } return true } 
+3
source

Swift 3

 class SignUpViewController: UIViewController, UITextFieldDelegate { lazy var emailTextField: UITextField = { let tf = UITextField() tf.translatesAutoresizingMaskIntoConstraints = false tf.placeholder = "Email address" tf.delegate = self return tf }() func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { let whitespaceSet = NSCharacterSet.whitespaces if let _ = string.rangeOfCharacter(from: whitespaceSet) { return false } else { return true } } } 
+2
source
 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { let whitespaceSet = CharacterSet.whitespaces if let _ = string.rangeOfCharacter(from: whitespaceSet) { return false } else { return true } } 
+2
source

Source: https://habr.com/ru/post/978790/


All Articles