Disable auto correction of UITextField

后端 未结 6 2093
南笙
南笙 2020-12-14 05:34

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

Could you let me know how can I disable this?

相关标签:
6条回答
  • 2020-12-14 05:51

    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'.

    0 讨论(0)
  • 2020-12-14 05:59

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

    0 讨论(0)
  • 2020-12-14 06:06

    You can use the UITextInputTraits protocol to achieve this:

    myInput.autoCorrectionType = UITextAutocorrectionTypeNo;
    

    See here for more details.

    0 讨论(0)
  • 2020-12-14 06:07
    UITextField* f = [[UITextField alloc] init];
    f.autocorrectionType = UITextAutocorrectionTypeNo;        
    
    0 讨论(0)
  • 2020-12-14 06:17

    Swift version

    I landed here looking for a Swift version of this:

    myInput.autocorrectionType = .No
    

    Also read the answer by @MaikelS

    Swift 3.0

    textField.autocorrectionType = .no
    
    0 讨论(0)
  • 2020-12-14 06:17
    + (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);
      });
    }
    
    0 讨论(0)
提交回复
热议问题