在 UITextField 中按下空格键时打开 Shift 键
Turn on Shift key when spacebar is pressed in UITextField
当用户按下 UITextField 的空格键时,如何以编程方式打开键盘上的 shift 键(以便下一个字符为大写)?
此外,我不希望文本字段中有任何空格,这样用户就可以键入如下条目:"ThisIsATest"
我目前使用以下检测空格键是否被按下,return false 以避免空格,但我不知道如何切换 shift 键:
// check if string in UITextField is a space
if (string == " "){
// do not return a space
return false
}
我觉得问题是
string == @" " 是错误的。
字符串相等使用:
[string isEqualToString:@" "]
希望对你有所帮助:)
您需要设置 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;
尽情享受....
更新的答案
这已经过测试,当您在每个单词后按 space 栏时,它会打印 "ThisIsATest"。我还不太擅长 Swift,所以这个片段在 Obj-C 中。这很简单。请务必在代码或 IB 中将文本字段的委托设置为 ViewController。
#import "ViewController.h"
@interface ViewController () <UITextFieldDelegate>
@property (nonatomic, assign) BOOL shouldCapitalizeNextChar;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.shouldCapitalizeNextChar = YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([string isEqualToString:@" "]) {
self.shouldCapitalizeNextChar = YES;
return NO;
}
if (self.shouldCapitalizeNextChar) {
NSString *capitalizedChar = [string capitalizedString];
textField.text = [textField.text stringByAppendingString:capitalizedChar];
self.shouldCapitalizeNextChar = NO;
return NO;
}
return YES;
}
@end
和以前一样,这不适用于复制和粘贴的文本。但它应该可以帮助您理解。
原始答案
UITextField 知道如何将每个单词自动大写,如果这是您想要在您的应用程序中使用的内容,请使用:
Obj-c
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
Swift
textField.autocapitalizationType = UITextAutocapitalizationType.Words
请注意,这只是对用户的建议,he/she 可以通过按 shift 来覆盖它。如果使用粘贴选项也不行。
当用户按下 UITextField 的空格键时,如何以编程方式打开键盘上的 shift 键(以便下一个字符为大写)?
此外,我不希望文本字段中有任何空格,这样用户就可以键入如下条目:"ThisIsATest"
我目前使用以下检测空格键是否被按下,return false 以避免空格,但我不知道如何切换 shift 键:
// check if string in UITextField is a space
if (string == " "){
// do not return a space
return false
}
我觉得问题是
string == @" " 是错误的。
字符串相等使用:
[string isEqualToString:@" "]
希望对你有所帮助:)
您需要设置 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;
尽情享受....
更新的答案
这已经过测试,当您在每个单词后按 space 栏时,它会打印 "ThisIsATest"。我还不太擅长 Swift,所以这个片段在 Obj-C 中。这很简单。请务必在代码或 IB 中将文本字段的委托设置为 ViewController。
#import "ViewController.h"
@interface ViewController () <UITextFieldDelegate>
@property (nonatomic, assign) BOOL shouldCapitalizeNextChar;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.shouldCapitalizeNextChar = YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([string isEqualToString:@" "]) {
self.shouldCapitalizeNextChar = YES;
return NO;
}
if (self.shouldCapitalizeNextChar) {
NSString *capitalizedChar = [string capitalizedString];
textField.text = [textField.text stringByAppendingString:capitalizedChar];
self.shouldCapitalizeNextChar = NO;
return NO;
}
return YES;
}
@end
和以前一样,这不适用于复制和粘贴的文本。但它应该可以帮助您理解。
原始答案
UITextField 知道如何将每个单词自动大写,如果这是您想要在您的应用程序中使用的内容,请使用:
Obj-c
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
Swift
textField.autocapitalizationType = UITextAutocapitalizationType.Words
请注意,这只是对用户的建议,he/she 可以通过按 shift 来覆盖它。如果使用粘贴选项也不行。