iOS 11 - disable smart quotes

本秂侑毒 提交于 2019-12-01 03:09:06

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!