iOS 11 - disable smart quotes

后端 未结 3 1548
清酒与你
清酒与你 2021-01-07 22:57

iOS 11 adds smart quotes when typing. In macOS we can disable smart quotes on a NSTextView by setting:

textView.automaticQuoteSubstitutionEnable         


        
相关标签:
3条回答
  • 2021-01-07 23:15

    I don't think smartQuotesType and smartQuotesType are good practices for some languages.

    Set these properties for every text inputs in our app:

    if (@available(iOS 11.0, *)) {
        textView.smartDashesType = UITextSmartDashesTypeNo;
        textView.smartQuotesType = UITextSmartQuotesTypeNo;
        textView.smartInsertDeleteType = UITextSmartInsertDeleteTypeNo;
    } else {
        // Fallback on earlier versions
    }
    

    Creating a category to disable these "SMART" features makes no sense (bug):

    - (UITextSmartDashesType)smartDashesType {
        return UITextSmartDashesTypeNo;
    }
    - (UITextSmartQuotesType)smartQuotesType {
        return UITextSmartQuotesTypeNo;
    }
    - (UITextSmartInsertDeleteType)smartInsertDeleteType {
        return UITextSmartInsertDeleteTypeNo;
    }
    

    So I tried to disable these features permanently by method swizzling:

    #import <objc/runtime.h>
    
    @implementation DisableFuckingSmartPunctuation
    
    + (void)load {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            Class class = [objc_getClass("UITextInputController") class];
            Class myClass = [self class];
    
            SEL originalSelector = @selector(checkSmartPunctuationForWordInRange:);
            SEL swizzledSelector = @selector(checkSmartPunctuationForWordInRange:);
    
            Method originalMethod = class_getInstanceMethod(class, originalSelector);
            Method swizzledMethod = class_getInstanceMethod(myClass, swizzledSelector);
    
            BOOL didAddMethod =
            class_addMethod(class,
                            originalSelector,
                            method_getImplementation(swizzledMethod),
                            method_getTypeEncoding(swizzledMethod));
    
            if (didAddMethod) {
                class_replaceMethod(class,
                                    swizzledSelector,
                                    method_getImplementation(originalMethod),
                                    method_getTypeEncoding(originalMethod));
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod);
            }
        });
    }
    
    - (void)checkSmartPunctuationForWordInRange:(id)arg1 {
    
    }
    
    @end
    

    Hacking private methods always works like a charm...

    0 讨论(0)
  • 2021-01-07 23:17

    Smart quotes and other features such as smart dashes are controlled via the UITextInputTraits Protocol which is adopted by both UITextField and UITextView.

    Specifically, the smartQuotesType property can be set to one of .default, .yes or .no. At this time there is no further documentation on these values, but .yes and .no are self-explanatory. My guess on .default is that the system will use properties such as textContentType and isSecureTextEntry to determine the appropriate behaviour.

    For example a text content type of email, password or URL would probably disable smart quotes by default while job title may default to enabled. I imagine secure text entry fields would also have smarts disabled by default.

    Setting an appropriate text content type for your input views can significantly improve the user experience and is highly recommended.

    0 讨论(0)
  • 2021-01-07 23:22

    I had a problem with this - I frequently use Prompt and NX via my iPad Pro. Turning off "smart punctuation" in settings worked.

    0 讨论(0)
提交回复
热议问题