UITextField auto-capitalization type - iPhone App

送分小仙女□ 提交于 2019-12-03 02:06:09

问题


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


回答1:


Use textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

For more information please read: UITextInputTraits Protocol Reference




回答2:


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:

  • 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:

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




回答3:


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];



回答4:


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




回答5:


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

textField.autocapitalizationType = UITextAutocapitalizationType.words

or shorter way

textField.autocapitalizationType = .words



回答6:


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".




回答7:


In Xamarin.ios / Monotouch this worked for me:

textField.AutocapitalizationType = UITextAutocapitalizationType.Words;


来源:https://stackoverflow.com/questions/6943016/uitextfield-auto-capitalization-type-iphone-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!