gpt4 book ai didi

ios - 在 UITextField 中按下空格键时打开 Shift 键

转载 作者:可可西里 更新时间:2023-11-01 01:05:54 33 4
gpt4 key购买 nike

当用户按下 UITextField 的空格键时,如何以编程方式打开键盘上的 shift 键(以便下一个字符为大写)?

此外,我不希望文本字段中有任何空格,这样用户就可以键入这样的条目:“ThisIsATest”

我目前使用以下方法检测空格键是否被按下,并返回 false 以避免出现空格,但我不知道如何切换 shift 键:

// check if string in UITextField is a space
if (string == " "){
// do not return a space
return false
}

最佳答案

您需要设置 textField 的 autoCapitalizationType。根据您的要求,您需要设置 Words

来自Docs :

Discussion This property determines at what times the Shift key is automatically pressed, thereby making the typed character a capital letter. The default value for this property is UITextAutocapitalizationTypeSentences.

SWIFT

enum UITextAutocapitalizationType : Int {
case None
case Words
case Sentences
case AllCharacters
}

Constants (For All : Available in iOS 2.0 and later.)

None : Do not capitalize any text automatically.

Words : Capitalize the first letter of each word automatically.

Sentences : Capitalize the first letter of each sentence automatically.

AllCharacters : Capitalize all characters automatically.

objective-c

typedef enum : NSInteger {
UITextAutocapitalizationTypeNone,
UITextAutocapitalizationTypeWords,
UITextAutocapitalizationTypeSentences,
UITextAutocapitalizationTypeAllCharacters,
} UITextAutocapitalizationType;

Constants (For All : Available in iOS 2.0 and later.)

UITextAutocapitalizationTypeNone : Do not capitalize any text automatically.

UITextAutocapitalizationTypeWords : Capitalize the first letter of each word automatically.

UITextAutocapitalizationTypeSentences : Capitalize the first letter of each sentence automatically.

UITextAutocapitalizationTypeAllCharacters : Capitalize all characters automatically.

就像:

SWIFT

textField.autocapitalizationType = UITextAutocapitalizationType.Words

objective-c

textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

享受....

关于ios - 在 UITextField 中按下空格键时打开 Shift 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30434653/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com