70

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?

8 Answers 8

153
UITextField* f = [[UITextField alloc] init];
f.autocorrectionType = UITextAutocorrectionTypeNo;        
2
  • 3
    Also if yo want autocapitalization off: f.autocapitalizationType = UITextAutocapitalizationTypeNone; Jul 21, 2015 at 15:49
  • Hello @Caffeine, How we can enable the same option for NSTextfield (Mac app)?
    – Surezz
    Dec 15, 2016 at 7:12
34

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
14

You can use the UITextInputTraits protocol to achieve this:

myInput.autoCorrectionType = UITextAutocorrectionTypeNo;

See here for more details.

10

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

7

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 here enter image description here

1
+ (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);
  });
}
1

In SwiftUI, you can use the .disableAutocorrection(true) modifier.

Hiere is a real life example:

VStack {
    TextField("title", text: $LoginModel.email)
        .autocapitalization(.none)
        .disableAutocorrection(true)
        .foregroundColor(.white)
}
0

Swift 5:

This is how I am achieving for email address field in my project

private let emailTextField: UITextField = {
    let tf = CustomTextField(placeholder: "Email address")
    tf.keyboardType = .emailAddress
    tf.autocorrectionType = .no        //disable auto correction
    tf.autocapitalizationType = .none   //disable default capitalization
    return tf
}()

CustomTextField is my extension class (not important here)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.