I've a number of textFields, I want to change text colour to white when user is typing them in text field. Following is my code with a lot of if conditions which doesn't seem to be efficient. Is there any way to do it without writing a lot of if conditions?
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == emailTextField {
emailTextField.textColor = .white
} else if textField == nameTextField {
nameTextField.textColor = .white
} else if textField == addressTextField {
addressTextField.textColor = .white
}
return true
}
Just do
textField.textColor = .white
and whatever the textfield is it's textColor will be changed
Related
I have a textfield with a attributed target which is supposed to detect for changes to the textfield label, and then run a function. However, the .editingChanged UIControl event is not triggered when a user swipes to type (on the keyboard); this also happens when a email or password is autofilled.
My code:
let textField: UITextField = {
let textField = UITextField()
textField.backgroundColor = .clear
textField.font = UIFont.systemFont(ofSize: 14, weight: .medium)
textField.textColor = .label
textField.placeholder = ""
return textField
}()
textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
#objc func textFieldDidChange(_ textField: UITextField) {
print(textField.text)
}
Have you tried including textField.sendActions(for: .editingChanged)?
It's also possible that textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) might be returning false?
I want to enable and change color of a button depending on text in password and confirm password textfields. So I found this code online:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let tfConfirmPasswordText = (tfConfirmPassword.text! as NSString).replacingCharacters(in: range, with: string)
if tfConfirmPasswordText.isEmpty {
btnContinue.backgroundColor = UIColor(named: "labelColor")
btnContinue.isEnabled = false
}
else if tfPassword.text != "" && !tfConfirmPasswordText.isEmpty {
btnContinue.backgroundColor = UIColor(named: "accentColor")
btnContinue.isEnabled = true
}
return true
}
This code does work and using these conditions it will enable or disable continue button according to confirmPassword textfield.
But the problem is that after filling both password and confirmPassword textfields, if I remove text from password field it still keeps the button enabled and only disables it if text is removed from confirmPassword textfield.
And if I put password.delegate = self alongside confirmPassword.delgate = self in viewDidLoafd(), it crashes when I put more than 1 character in any textfield.
Is there any way to enable or disable button by always keeping a check on both the textfields instead of just one..ie. confirmPassword textfield?
Instead of using shouldChangeCharactersIn ,i used like below to make it works
override func viewDidLoad() {
super.viewDidLoad()
//initially at disabled
disabledButton()
[tfPassword, tfConfirmPasswordText].forEach({ $0.addTarget(self, action: #selector(textFieldEditingDidChange), for: .editingChanged) })
}
#objc func textFieldEditingDidChange(sender: UITextField) {
guard
let tfPasswordTxtValue = tfPassword.text, !tfPasswordTxtValue.isEmpty,
let tfConfirmPasswordTextValue = tfConfirmPasswordText.text, !tfConfirmPasswordTextValue.isEmpty
else {
disabledButton()
return
}
enableButton()
}
func disabledButton(){
btnContinue.backgroundColor = UIColor(named: "labelColor")
btnContinue.isEnabled = false
}
func enableButton(){
btnContinue.isEnabled = true
}
I think you're missing a test:
Instead of
if tfConfirmPasswordText.isEmpty
I would have write
if tfConfirmPasswordText.isEmpty || tfpasswordText.isEmpty
i'm designing login and signUp screen for email-field i want restrict user to write Uppercase characters in textField.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if (string.rangeOfCharacter(from: CharacterSet.uppercaseLetters) != nil) {
return false
}
return true
}
This code worked fine for input restriction.
But i want that user can't type it from keybord. For keybord i'm using IQKeyboardManagerSwift.
Please give me some solution for this.
Thank you.
textField.autocapitalizationType = .none
textfield.autocorrectionType = .no
textField.spellCheckingType = .no
Then:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let _ = string.rangeOfCharacter(from: .uppercaseLetters) {
// Don't allow upper case letters
return false
}
return true
}
I am trying text field first character show lowercase, if I can type the upper case character also it has to be show the lower case. I tried this
func textField(_ textFieldToChange: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
signUpEmailTextField.autocapitalizationType = .none
}
But it's not working for me, my aim is when I type capital character also it should show the lower case in textfield.
A simple solution is to add target for when text changes and update text to lowercased there:
override func viewDidLoad() {
super.viewDidLoad()
signUpEmailTextField.addTarget(self, action: #selector(textFieldChanged), for: .editingChanged)
}
#objc func textFieldChanged() {
signUpEmailTextField.text = signUpEmailTextField.text?.lowercased()
}
with uppercased() you can achieve this
you can also check condition if you have multiple textfields and you don't want uppercased in all text field
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text,
let range = Range.init(range, in: text) {
let newText = text.replacingCharacters(in: range, with: string).uppercased()
textField.text = newText
}
}
I want to perform some action when my textField character count becomes 6 in swift.
Use the delegate
textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
and then implement
func textFieldDidChange(_ textField: UITextField) {
if textField.text?.characters.count == 6 {
// Call some method
}
}
You can use shouldChangeCharactersInRange to get length as well as position using range.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let text = (textField.text ?? "") as String
let txtAfterUpdate = text.replacingCharacters(in: range, with: string)
if (text.characters.count == 6 && range.location > 5) {
return false // if you want to restrict your string length of call some method here
}
return true
}