UITextField autocapitalizationType UITextAutocapitalizationTypeAllCharacters not working on the device

You can set the autocapitalizationType UITextField property so that all input is uppercase. I find that works fine on the simulator (when you press the simulator keyboard, not the Mac keyboard), but not on the device? Everything stays lowercase.

In the UICatalog demo, I added to the textFieldNormal method:

textFieldNormal.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters; 

Added delegate to display actual autocapitalizationType for UITextField:

 - (void)textFieldDidBeginEditing:(UITextField *)textField { NSLog( @"textField.autocapitalizationType=%d", textField.autocapitalizationType ); } 

It will display 3 correctly (= UITextAutocapitalizationTypeAllCharacters), but everything you click remains lowercase. What am I missing?

+6
source share
2 answers

Apparently, this is a problem with the general settings of the device: Settings โ†’ General โ†’ Keyboard โ†’ Auto-Capitalization must be enabled to comply with the textField.autocapitalizationType setting for all uppercase letters, otherwise the property setting is ignored, apparently. If I turn it on, everything will work as expected.

+19
source

You can try something like a text field delegate:

 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (range.length == 0) { // not deleting , but adding a char textField.text = [textField.text stringByAppendingString:[string uppercaseString]]; return NO; } return YES; } 

This only works if you try to insert a character at the end of the text. If you want to play with the text in the middle, you can play with

range.location

and also you will need to play with the cursor positioning, as it will end each time ...

I hope this helps someone.

0
source

Source: https://habr.com/ru/post/950345/


All Articles