Skip to content

Instantly share code, notes, and snippets.

@y8k
Created September 1, 2015 14:05
Show Gist options
  • Save y8k/f6c02723b8b036b2231f to your computer and use it in GitHub Desktop.
Save y8k/f6c02723b8b036b2231f to your computer and use it in GitHub Desktop.
UITextField delegate
func textFieldDidBeginEditing(textField: UITextField) {
self.showMessageToUser(nil)
}
func textFieldDidEndEditing(textField: UITextField) {
if textField.text?.characters.count <= 0 {
if let hasLabel = textField as? UTTextField {
hasLabel.changeStatus(.Normal)
}
}
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if string.characters.count == 0 {
if textField.text?.characters.count <= 1 {
if let hasLabel = textField as? UTTextField {
hasLabel.changeStatus(.Normal)
}
}
}
else {
if let hasLabel = textField as? UTTextField {
hasLabel.changeStatus(.Input)
}
}
return true
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField.isEqual(self.emailTextField) {
self.passwordTextField.becomeFirstResponder()
}
else {
textField.resignFirstResponder()
}
return false
}
func textFieldShouldClear(textField: UITextField) -> Bool {
if let hasLabel = textField as? UTTextField {
hasLabel.changeStatus(.Normal)
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment