ios UITextField 自动大写类型 - iPhone 应用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6943016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:57:54  来源:igfitidea点击:

UITextField auto-capitalization type - iPhone App

iosuitextfield

提问by CodeGuy

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

有没有办法为 a 设置 autocapitalizationTypeUITextField以便每个单词的第一个字母默认大写?

This Is An Example Of What I Want

这是我想要的一个例子

回答by AlexVogel

Use textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

For more information please read: UITextInputTraits Protocol Reference

更多信息请阅读:UITextInputTraits 协议参考

回答by nhgrif

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

我们要做的是设置文本字段的autocapitalizationType.

Objective-C:

目标-C:

self.textfield.autocapitalizationType = UITextAutocapitalizationTypeWords

Swift:

迅速:

self.textfield.autocapitalizationType = .words

There are a few options here:

这里有几个选项:

enter image description here

在此处输入图片说明

  • allCharactersis the same as double tapping the shift key, basically capslock.
  • noneis pretty self-explanatory, keyboard will never try to capitalize letters.
  • sentenceswill try to capitalize the next word after an end mark punctuation.
  • wordswill try to capitalize every new word (after a space), which seems to be exactly what you're looking for.
  • allCharacters与双击shift键相同,基本上是大写锁定。
  • none不言自明,键盘永远不会尝试大写字母。
  • sentences将尝试将结束标记标点后的下一个单词大写。
  • words将尝试大写每个新单词(在空格之后),这似乎正是您要查找的内容。

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.

“大写”是该组中的第一个选项,只是通过选择文本字段的最小字体大小。

回答by Evan Mulawski

Setting the capitalizationproperty to "Words" will suggestthat 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:

将该capitalization属性设置为“Words”将建议用户输入大写单词。这可以由用户通过在键盘上取消切换来覆盖。最好的办法是将代码中的单词大写:

NSString *text = [myTextField.text capitalizedString];

回答by Marsson

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

是的。在InterfaceBuilder 上,在textField 属性检查器上,您可以将Capitalization 属性设置为Words。

回答by Kemal Can Kaynak

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

Obj-C 有一个答案,我是 Swift 的答案;

textField.autocapitalizationType = UITextAutocapitalizationType.words

or shorter way

或更短的方式

textField.autocapitalizationType = .words

回答by mgm

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

您甚至可以通过 Storyboard 或 .xib 文件执行此操作。只需选择文本字段,在右侧的属性检查器中有一个大写部分,您可以在其中选择适合您的任何内容。在您的情况下,它是大写的“单词”。

回答by Daniele D.

In Xamarin.ios / Monotouch this worked for me:

在 Xamarin.ios / Monotouch 这对我有用:

textField.AutocapitalizationType = UITextAutocapitalizationType.Words;