Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField auto-capitalization type - iPhone App

Is there a way to set the autocapitalizationType for a UITextField so that the first letter of each word is capitalized by default?

This Is An Example Of What I Want

like image 733
CodeGuy Avatar asked Aug 04 '11 14:08

CodeGuy


People also ask

How do you auto capitalize in text?

To use a keyboard shortcut to change between lowercase, UPPERCASE, and Capitalize Each Word, select the text and press SHIFT + F3 until the case you want is applied.


2 Answers

Use textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

For more information please read: UITextInputTraits Protocol Reference

like image 78
AlexVogel Avatar answered Oct 02 '22 12:10

AlexVogel


What we want to do is set the text field's autocapitalizationType.

Objective-C:

self.textfield.autocapitalizationType = UITextAutocapitalizationTypeWords 

Swift:

self.textfield.autocapitalizationType = .words 

There are a few options here:

enter image description here

  • allCharacters is the same as double tapping the shift key, basically capslock.
  • none is pretty self-explanatory, keyboard will never try to capitalize letters.
  • sentences will try to capitalize the next word after an end mark punctuation.
  • words will try to capitalize every new word (after a space), which seems to be exactly what you're looking for.

This property can also be set from the Attributes Inspector of the interface builder when you have the appropriate text field selected:

enter image description here

"Capitalization" is the first option in this group, just passed picking the minimum font size for the text field.

like image 40
nhgrif Avatar answered Oct 02 '22 12:10

nhgrif