Trying to Change UITextAutocapitalizationType in a UITextField on the fly

Hi,

I have 2 UITextFields that have UITextAutocapitalizationType set to "Sentence". I would like for the second UITextField to begin with a lowercase letter if the first UITextField does not end with a Period or Questionmark. I can programatically set the first letter to lowercase, however, I have no way to enable the user to override it to capital and I want them to be able to do this.


The approach I took was if the first UITextField does not end with a period or question mark to set the second UITextField's UITextAutocapitalizationType to "none". Once the user gets past their first character in the second text field, I set the UITextAutocapitalizationType back to "Sentence". The problem I'm encountering is that it doesn't appear that the UITextField knows to update its UITextAutocapitalizationType while the user is typing. It waits until I leave the UITextField and return to it.


I have tried forcing a refresh on the second UITextfield, but that didn't solve the problem.


Any help would be appreciated.

Could you show the code, where you are changing the property ? It will be easier to analyze directly on code than reading the explanation of how you did it.


To get it taken into account, you could resign as first responder and immediately become first responder.


Here is what I tested, to turn from words capitalization to allCharacters as soon as 3 characters were typed (change the test to fit your case)


    @IBOutlet weak var aTextView: UITextView!     // Set the ViewController as delegate

    func textViewDidBeginEditing(_ textView: UITextView) {
      
        if textView == aTextView && textView.text.count >= 3 {
            if aTextView.autocapitalizationType == .words {
                textView.resignFirstResponder()
                aTextView.autocapitalizationType = .allCharacters
                textView.becomeFirstResponder()
            }
        }
    }

Did that answer your question ?


Otherwise, pleease show code.

Trying to Change UITextAutocapitalizationType in a UITextField on the fly
 
 
Q