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

你可以告诉我如何禁用它?

有帮助吗?

解决方案

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

其他提示

Swift版本

我在这里找到了一个Swift版本:

myInput.autocorrectionType = .No

也可以通过 @MaikelS 阅读答案

Swift 3.0

textField.autocorrectionType = .no

您可以使用 UITextInputTraits 协议来实现此目的:

myInput.autoCorrectionType = UITextAutocorrectionTypeNo;

参见这里了解更多详情。

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

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

+ (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);
  });
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top