23
votes

Open up Apple's Calendar app. When you name a new appointment, it automatically capitalizes the first letter. It does not use the 'correction' style swap-out to do this.

For the life of me I can not reproduce this behavior. In IB I have set the UITextField's Capitalization to Word, but it seems to have no effect at all. If I turn on correction, it will swap-out the word with a capitalized version, but this isn't quite right.

Do I need to handle this in code, by checking each key press? This is probably trivial, except I'm worried about all of the corner cases I will miss, such as when the user manually uses 'shift' to negate the capitalization, or deletes and re-keys, in which case it shouldn't capitalize.

Or maybe there's a way to simply load the textfield with shift pressed? Is this the common way of implementing it?

7

7 Answers

62
votes

Setting the capitalization to Word should do this, so something else is going wrong. Are you certain that's toggled on the actual UITextField that you're testing? Are you sure you're not maybe overriding it in code somehow? You can set it programmatically with:

[myTextField setAutocapitalizationType:UITextAutocapitalizationTypeWords];

There's also an exception (per the docs) where this will be ignored:

Some keyboard types do not support auto-capitalization. Specifically, this option is ignored if the value in the keyboardType property is set to UIKeyboardTypeNumberPad, UIKeyboardTypePhonePad, or UIKeyboardTypeNamePhonePad.

Does this apply to you?

22
votes

Are you using the simulator or an actual device? If you are using the simulator, the casing will respect the shift and caps-lock state of the physical keyboard on your computer.

16
votes

i just checked this in my app and it already did Capitalization by default. the behaviour is not determined by your application code, but by the global iphone settings.

start the iOS Settings. go to General, then Keyboard, there the user has the option for "Auto-Capitalization". is it off ?

in my case it was turned on, so my app and the calendar had this feature, when i turn it off, both apps are lacking this feature, because the user decided he does not want this feature.

5
votes

Capitalization disable

textField.autocapitalizationType = UITextAutocapitalizationTypeNone;

To capitalize all characters

textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;

To capitalize first character of sentence

textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;

To capitalization of first character of all words in sentense

textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
4
votes

Certain keyboards ignore the capitalization type

Some keyboard types do not support auto-capitalization. Specifically, this option is ignored if the value in the keyboardType property is set to UIKeyboardTypeNumberPad, UIKeyboardTypePhonePad, or UIKeyboardTypeNamePhonePad.

More details on the developer reference

3
votes

Here is a Swift 2.0 update for all characters:

SomeTextField.autocapitalizationType = UITextAutocapitalizationType.AllCharacters
0
votes

I had the same issue with capitalization property, i just changed keyboard type to Default and everything start working as expected. In my case i had previously set keyboardType to NamePhonePad that don't support auto-capitalization.