Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable the "Space" Key on the keyboard in Swift?

I'm creating a login system and I don't want spaces to be allowed in the username field. Instead of check and validating the field, I want to prevent spaces from being added.

So, every time the user presses the spacebar, nothing should happen. How can I do that? I've seen Instagram do this.

This is my code so far:

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
    }
}
like image 259
Davide Colla Avatar asked Nov 27 '14 13:11

Davide Colla


People also ask

How do I turn off keyboard Swift?

Via Tap Gesture This is the quickest way to implement keyboard dismissal. Just set a Tap gesture on the main View and hook that gesture with a function which calls view. endEditing . Causes the view (or one of its embedded text fields) to resign the first responder status.

How do I enable the keyboard in Swift?

To enable SwiftKey and make it your default keyboard on stock Android, head to settings, select Language & input, and choose SwiftKey from the list of options. The keyboard is also available for free in Apple's App Store for iPhone, iPad, and iPod touch devices running iOS 8.


2 Answers

Swift 5

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    return string != " "
}

This is the most easy to understand syntax for me.

like image 142
Philipp Serfling Avatar answered Sep 28 '22 07:09

Philipp Serfling


Yes you can do it. Override UITextField delegate method shouldChangeCharactersInRange. And in that method check for space character. If found than return false.

Note: Don't forget to set delegate for textfield.

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 textField object is an IBOutlet. It means that text field control is in storyboard and connected with this variable.

like image 33
Kampai Avatar answered Sep 28 '22 07:09

Kampai