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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:01:10  来源:igfitidea点击:

iPhone: Issue disabling Auto-Cap/autocorrect on a UITextField

iosobjective-ciphoneuitextfield

提问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

见截图: 替代文字 http://grab.by/39WE

回答by Tom

You're setting autocorrectionTypeto FALSEas if it were a BOOL, but it actually has type UITextAutocorrectionType. So FALSEis being interpreted as UITextAutocorrectionTypeDefault, which means that autocorrection is probably enabled.

你设置autocorrectionTypeFALSE就好像它是一个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。

enter image description here

在此处输入图片说明

回答by Chris Peragine

Updated the correct answer for Swift 5

更新了 Swift 5 的正确答案

    textField.autocorrectionType = .no
    textField.autocapitalizationType = .none
    textField.spellCheckingType = .no