122

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

nhgrif
  • 61,578
  • 25
  • 134
  • 173
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • 45
    On the other hand - it is nice to be able to google 'UITextField capitalization' and have this pop up in 2 seconds, rather than wasting a full minute to open the documentation and scan for this. StackOverflow IS documentation. – Shaun Budhram Aug 27 '13 at 20:44
  • 1
    Stumbling upon questions like this makes me happy that 1) people are capable of asking coherent direct questions (no matter the scope) and 2) people comment the best s*** – Will Von Ullrich May 16 '17 at 02:12

7 Answers7

253

Use textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

For more information please read: UITextInputTraits Protocol Reference

malhal
  • 26,330
  • 7
  • 115
  • 133
AlexVogel
  • 10,601
  • 10
  • 61
  • 71
  • 2
    It doesn't help if you past text to field. In this case delegate text input and replace first letter of each word – HotJard Dec 10 '13 at 08:17
  • Would you care to elaborate some more HotJard? I d like to know how to paste a text and autocapitalize. – vnchopra Aug 24 '14 at 02:04
  • 5
    It doesn't work, i tried it on Xcode 6 iOS 8 Swift. I did it in code and interface builder, neither works. – Van Du Tran Nov 27 '14 at 21:26
  • @VanDuTran I noticed interface builder change worked on my phone, but not the simulator. ( XCode Version 6.2 (6C131e) ) – finneycanhelp Mar 28 '15 at 21:59
  • for me in swift this worked: `textField?.autocapitalizationType = UITextAutocapitalizationType.Words` – Kashif Apr 09 '15 at 18:55
  • 10
    Make sure the keyboard in interface builder is set to default. Otherwise it won't work – OscarVGG Aug 25 '15 at 15:50
  • @OscarVGG with the saaaaave. Thanks. That is what was causing my head scratches. – Jake T. Oct 21 '16 at 21:32
  • 3
    Also make sure you have autocapitalization enabled in the os settings (settings->general->keyboard), iOS settings values override anything you set on the text field – Lope Dec 20 '16 at 11:53
  • is there any update for xcode 11.7 or 2020 :) I am getting "Use of unresolved identifier 'textField' ", thanks – greenridinghood Sep 03 '20 at 01:52
  • @greenridinghood 'textField' is the name of the UITextField variable. Please change it to your used name. – AlexVogel Sep 14 '20 at 08:45
40

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.

Rafael
  • 7,002
  • 5
  • 43
  • 52
nhgrif
  • 61,578
  • 25
  • 134
  • 173
8

There is an answer for Obj-C, im here for Swift;

textField.autocapitalizationType = UITextAutocapitalizationType.words

or shorter way

textField.autocapitalizationType = .words
Kemal Can Kaynak
  • 1,638
  • 14
  • 26
  • I tested .words and .allCharacters. On the Simulator, it works. On a connected iPhone X, it doesn't. Xcode 10.1, iPhone 12.1. – geohei Dec 04 '18 at 17:19
  • 1
    I will not work if in setting -> General -> Auto-Correction / Auto-Capitalization is turned off. – Sheetal Shinde Sep 18 '19 at 10:15
8

Setting the capitalization property to "Words" will suggest that the user enter capitalized words. This can be overridden by the user by un-shifting on the keyboard. The best thing to do is capitalize the words in code:

NSString *text = [myTextField.text capitalizedString];
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
  • This will capitalize the entire string, UITextAutocapitalizationTypeWords just capitalizes the first letter in each word typed. – Jacob Dec 19 '14 at 06:52
  • Although this is not what the OP wanted, I really really found it useful! I had an NSString saved somewhere, which was all uppercased. I needed to compare a UITextField's text to the original string, and using your recommendation saved me from having to explain things to my user (I let Objective c do all the work : ) ) Thank you – Septronic Nov 02 '15 at 01:11
7

Yes. On InterfaceBuilder, on the textField attribute inspector, you can set up the Capitalization property to Words.

Marsson
  • 687
  • 7
  • 16
1

You can even do that form the Storyboard or .xib file. Just have to select the textfield and in the attribute inspector on the right there is a Capitalization section where you can pick whatever suits you. In your case it's capitalisation "Words".

mgm
  • 1,298
  • 14
  • 16
0

In Xamarin.ios / Monotouch this worked for me:

textField.AutocapitalizationType = UITextAutocapitalizationType.Words;
Daniele D.
  • 2,624
  • 3
  • 36
  • 42