iOS 11 - disable smart quotes

后端 未结 3 1550
清酒与你
清酒与你 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 
    
    @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...

提交回复
热议问题