Turn off Autocorrect Globally in an App

后端 未结 4 1589
再見小時候
再見小時候 2021-01-24 12:21

I would like to disable text-entry autocorrect in an iPad application, regardless of what the global settings for autocorrect are on the device. Is there a good way to do this t

相关标签:
4条回答
  • 2021-01-24 12:57

    Im sorry but you have to go trough all the text fields and disable it

    0 讨论(0)
  • 2021-01-24 13:06

    You can probably subclass UITextField and set your desired properties to it. Instead of the UITextField you can use this subclassed version.

    This may be worthy if you haven't started implementing your application yet!

    0 讨论(0)
  • 2021-01-24 13:11

    as @cocoakomali suggested, you can create a category of UITextField to disable autocorrect for all UITextField in the app by default

    @implementation UITextField (DisableAutoCorrect)
    
    - (instancetype)init {
      self = [super init];
      if (self) {
        [self setAutocorrectionType:UITextAutocorrectionTypeNo];
      }
      return self;
    }
    
    @end
    
    0 讨论(0)
  • 2021-01-24 13:14

    You can override the default text field autocorrection type with a bit of method swizzling. In your app delegate or somewhere else sensible:

    #import <objc/runtime.h>
    
    // Prevent this code from being called multiple times
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        struct objc_method_description autocorrectionTypeMethodDescription = protocol_getMethodDescription(@protocol(UITextInputTraits), @selector(autocorrectionType), NO, YES);
        // (Re)implement `-[UITextField autocorrectionType]` to return `UITextAutocorrectionTypeNO`.
        IMP noAutocorrectionTypeIMP = imp_implementationWithBlock(^(UITextField *_self){ return UITextAutocorrectionTypeNo; });
        class_replaceMethod([UITextField class], @selector(autocorrectionType), noAutocorrectionTypeIMP, autocorrectionTypeMethodDescription.types);
    });
    
    0 讨论(0)
提交回复
热议问题