Turn off Autocorrect Globally in an App

后端 未结 4 1591
再見小時候
再見小時候 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 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 
    
    // 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);
    });
    

提交回复
热议问题