UITextInputTraits
我构建了一个测试工具来检查这一点,并发现根据我在协议中设置的值,我会得到不同的听写结果。具体来说:
myTextField.autocorrectionType = UITextAutocorrectionTypeYes; // or UITextAutocorrectionTypeNo
myTextField.spellCheckingType = UITextSpellCheckingTypeYes; // or UITextSpellCheckingTypeNo
影响 UITextField 的听写结果。请注意,UITextField 和 UITextView 的默认值为:
UITextAutocorrectionTypeDefault
UITextSpellCheckingTypeDefault
UITextInputTraits文档没有这么说,但我想 UITextField 和 UITextView 之间的默认行为可能会有所不同,或者(更有可能)自动更正在某些字段(例如搜索框)上关闭,甚至(如 @ 所提到的) Ricky 在他的回答中)说应用程序开发人员选择在呈现给用户之前更改结果。
在 iOS7.1 下测试,我可以将 UITextField 和 UITextView 的“逗号”转换为“,”。此外,即使打开了自动更正功能,我的发音也意味着有时我的 UITextField 和 UITextView 都得到了“Indy”而不是“ND”。
更新:
为了完整起见,下面是一个工作测试工具,它支持 UITextField 和 UITextView 将“逗号”转换为“,”。正如 OP 在他自己的回答中所暗示的那样 - 不需要设置自动更正和拼写检查来使其正常工作。
@implementation TestHarnessViewController {
UITextField *myTextField;
UITextView *myTextView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor grayColor]];
myTextField = [UITextField new];
[myTextField setFrame:CGRectMake(10, 100, 283, 23)];
myTextField.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myTextField];
myTextView = [UITextView new];
[myTextView setFrame:CGRectMake(10, 200, 283, 100)];
[self.view addSubview:myTextView];
}
- (void)keyboardWillShow:(NSNotification *)note {
//
}
- (void)keyboardWillHide:(NSNotification *)note {
//
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return TRUE;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
return [textField resignFirstResponder];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
return TRUE;
}
@end