ios 禁用 UITextField 的自动更正

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/827310/
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 15:53:35  来源:igfitidea点击:

Disable auto correction of UITextField

iosobjective-cuitextfield

提问by ebaccount

When I try to edit texts in my iPhone application (UITextfield), it auto-corrects my input.

当我尝试在 iPhone 应用程序 ( UITextfield) 中编辑文本时,它会自动更正我的输入。

Could you let me know how can I disable this?

你能告诉我如何禁用它吗?

回答by George Armhold

UITextField* f = [[UITextField alloc] init];
f.autocorrectionType = UITextAutocorrectionTypeNo;        

回答by Dan Beaulieu

Swift version

迅捷版

I landed here looking for a Swift version of this:

我来到这里寻找 Swift 版本:

myInput.autocorrectionType = .No

Also read the answer by @MaikelS

另请阅读@MaikelS的答案

Swift 3.0

斯威夫特 3.0

textField.autocorrectionType = .no

回答by Jonas

You can use the UITextInputTraitsprotocol to achieve this:

您可以使用该UITextInputTraits协议来实现这一点:

myInput.autoCorrectionType = UITextAutocorrectionTypeNo;

See herefor more details.

请参阅此处了解更多详情。

回答by MaikelS

The Interface Builder also has a dropdown field to disable this. As you're more likely to create textfields in the interface builder, look for it there. You can find it in the Attributes Inspector next to 'Correction'.

Interface Builder 也有一个下拉字段来禁用它。由于您更有可能在界面构建器中创建文本字段,因此请在那里查找。您可以在“更正”旁边的属性检查器中找到它。

回答by gutte

you can also set this in the storyboard by choosing the 'attributes inspector' and under 'correction' you can choose: 'Default', 'yes' and 'no' enter image description hereenter image description here

您还可以通过选择“属性检查器”在故事板中进行设置,在“更正”下您可以选择:“默认”、“是”和“否” enter image description hereenter image description here

回答by Alok Singh

+ (void)disableAutoCorrectionsForTextfieldsAndTextViewGlobally {
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    struct objc_method_description autocorrectionTypeMethodDescription =
        protocol_getMethodDescription(@protocol(UITextInputTraits),
                                      @selector(autocorrectionType), NO, YES);
    IMP noAutocorrectionTypeIMP_TEXT_FIELD =
        imp_implementationWithBlock(^(UITextField *_self) {
          return UITextAutocorrectionTypeNo;
        });
    IMP noAutocorrectionTypeIMP_TEXT_VIEW =
        imp_implementationWithBlock(^(UITextView *_self) {
          return UITextAutocorrectionTypeNo;
        });
    class_replaceMethod([UITextField class], @selector(autocorrectionType),
                        noAutocorrectionTypeIMP_TEXT_FIELD,
                        autocorrectionTypeMethodDescription.types);
    class_replaceMethod([UITextView class], @selector(autocorrectionType),
                        noAutocorrectionTypeIMP_TEXT_VIEW,
                        autocorrectionTypeMethodDescription.types);
  });
}