ios iPhone:在 UITextField 上禁用自动大写/自动更正问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2474824/
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
iPhone: Issue disabling Auto-Cap/autocorrect on a UITextField
提问by phil swenson
For some reason, even though I disable the auto-cap and auto-correct of my UITextField, it's still capitalizing the first letter of my input.
出于某种原因,即使我禁用了 UITextField 的自动大写和自动更正,它仍然大写我输入的第一个字母。
Here is the code:
这是代码:
UITextField* textField = [[[UITextField alloc] initWithFrame:CGRectMake(90.0, 10.0, 213.0, 25.0)] autorelease];
[textField setClearButtonMode:UITextFieldViewModeWhileEditing];
textField.returnKeyType = UIReturnKeyGo;
textField.autocorrectionType = FALSE;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.delegate = self;
if (inputFieldType == Email) {
label.text = @"Email:";
textField.keyboardType = UIKeyboardTypeEmailAddress;
emailTextField = textField;
textField.placeholder = @"Email Address";
} else { // password
textField.secureTextEntry = TRUE;
label.text = @"Password:";
if (inputFieldType == Password){
textField.placeholder = @"Password";
passwordTextField = textField;
}
if (inputFieldType == ConfirmPassword){
textField.placeholder = @"Confirm Password";
confirmPasswordTextField = textField;
}
}
See screenshot: alt text http://grab.by/39WE
回答by Tom
You're setting autocorrectionType
to FALSE
as if it were a BOOL
, but it actually has type UITextAutocorrectionType
. So FALSE
is being interpreted as UITextAutocorrectionTypeDefault
, which means that autocorrection is probably enabled.
你设置autocorrectionType
到FALSE
就好像它是一个BOOL
,但它实际上有型UITextAutocorrectionType
。所以FALSE
被解释为UITextAutocorrectionTypeDefault
,这意味着可能启用了自动更正。
I bet it found the name "Phil" in your address book and is autocorrecting the capitalization to match.
我敢打赌,它在你的地址簿中找到的名称“菲尔”,并自动更正大小写相匹配。
回答by Anbu.Karthik
Objective - C
目标-C
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
textField.autocorrectionType = FALSE; // or use UITextAutocorrectionTypeNo
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
}
Swift
迅速
func textFieldDidBeginEditing(textfield : UITextField)
{
textField.autocorrectionType = .No
textField.autocapitalizationType = .None
textField.spellCheckingType = .No
}
Swift3
斯威夫特3
func textFieldDidBeginEditing(_ textField : UITextField)
{
textField.autocorrectionType = .no
textField.autocapitalizationType = .none
textField.spellCheckingType = .no
}
回答by Subham93
yourTextFieldName.autocorrectionType = UITextAutocorrectionTypeNo;
yourTextFieldName.autocapitalizationType = UITextAutocapitalizationTypeNone;
回答by Maxamilian Demian
Swift 2:
斯威夫特 2:
textField.autocorrectionType = .No
textField.autocapitalizationType = .None
回答by Vikash Sinha
If you wish to disable Auto-Cap/autocorrect on a UITextField for whole project,
如果您希望在整个项目的 UITextField 上禁用自动大写/自动更正,
then make a class which will inherit the UITextField class and init method set the autoCorrectionType to "no".
然后创建一个将继承 UITextField 类和 init 方法的类,将 autoCorrectionType 设置为“no”。
Class AppTextField: UITextField {
override init(frame: CGRect) {
super.init(frame: frame)
//setting the autocorrection to no
self.autocorrectionType = .no
}
}
Then in storyboard set the cusotom class for textfield to AppTextField.
然后在情节提要中将文本字段的自定义类设置为 AppTextField。
回答by Chris Peragine
Updated the correct answer for Swift 5
更新了 Swift 5 的正确答案
textField.autocorrectionType = .no
textField.autocapitalizationType = .none
textField.spellCheckingType = .no