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
}
}
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With