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.
source share