-1

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.

M Reza
  • 18,350
  • 14
  • 66
  • 71
User1985
  • 19
  • 4
  • Possible duplicate of [Force a UITextField to lowercase while typing and retaining cursor position](https://stackoverflow.com/questions/35548633/force-a-uitextfield-to-lowercase-while-typing-and-retaining-cursor-position) – prex Feb 09 '19 at 07:14

2 Answers2

1

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()
}
M Reza
  • 18,350
  • 14
  • 66
  • 71
0

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
    }
}
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98