如何为特定文本字段预选大写锁定?

How can I preselect Caps lock for a specific textField?

当我开始打字时,我希望在键盘上预先选择 CapsLock,所以我只需在 Caps lock 中输入所有内容。

来自苹果 docs

The UITextInputTraits protocol defines features associated with keyboard input to a text object. For a custom text object to support keyboard input, it must adopt this protocol to interact properly with the text input management system. The UITextField and UITextView classes automatically support this protocol.

基于文本的视图的自动大写行为。与 autocapitalizationType 属性.

一起使用
enum UITextAutocapitalizationType : Int {
case None
case Words
case Sentences
case AllCharacters
}

您可以通过以下方式实现所有字符的自动大写:

textField.autocapitalizationType = .allCharacters

如果您希望 textField 上的字母始终大写,即使用户切换了键盘上的 Caps Lock 按钮,您也可以使用 UITextFieldDelegate 的 textFieldDidChange 方法并将文本设置为始终大写,如下所示:

func textFieldDidChange(_ textField: UITextField) {
      textField.text = textField.text?.uppercased()
 }