UITextField autocapitalizationType UITextAutocapitalizationTypeAllCharacters not working on device

StackOverflow https://stackoverflow.com/questions/17879752

  •  04-06-2022
  •  | 
  •  

Question

You can set the autocapitalizationType property of a UITextField so all input is in upper case. I find that does work fine on the simulator (when actually tapping the simulator's keypad, not the Mac's keyboard), but not on the device? Everything stays lowercase.

In the UICatalog demo I added to the textFieldNormal method:

textFieldNormal.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;

Added a delegate too, to display the actual autocapitalizationType for the UITextField:

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

It will properly display 3 (=UITextAutocapitalizationTypeAllCharacters), but anything you tap remains lowercase. What am I missing?

Was it helpful?

Solution

Apparently this is a device general settings issue: Settings -> General -> Keyboard -> Auto-Capitalization must be ON to honour the setting of textField.autocapitalizationType to all upper case, else setting the property is ignored, apparently. If I switch it on everything works as expected.

OTHER TIPS

You could try something like this the the textfield 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 works only if you try to insert a symbol at the end of the text. Should you want to play with the text in the middle you could play with

range.location

and also you will need to play with the cursor positioning as it will go at the end every time...

I hope this helps someone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top