iOS 11 - 禁用智能引号

19

iOS 11 在输入时增加了智能引号。在 macOS 中,我们可以通过设置来禁用 NSTextView 上的智能引号:

textView.automaticQuoteSubstitutionEnabled = NO;  

在iOS 11上,似乎UITextFieldUITextView都没有此属性或enabledTextCheckingTypes属性。如何禁用智能引号?

3个回答

33

不错的补充。只是需要澄清一下:smartQuotesType 仅在 iOS11 中可用。Apple 文档 - Mr Spiegel
1
是的,但“iOS 11”实际上是问题标题和问题中的前两个单词,所以我认为这很清楚。 - Paulw11
没错,但是在iOS11设备上运行iOS10应用程序时,您可能会遇到此问题。您可能会遇到iOS11中的智能标点符号问题,除非您的项目在iOS11中编译,否则无法摆脱这些问题。 - Mr Spiegel
5
我还要补充一点:这种设置只能防止用户从键盘输入智能引号。但是,用户可以从其他应用程序中输入智能引号并将其复制/粘贴到你的smartQuotesType = .no文本字段中。真正防止智能引号的最佳方法是实现委托textField:shouldChangeCharactersInRange:replacementString:,检测智能引号并用普通的撇号替换它。 - micnguyen
@micnguyen 你如何检测智能引号? - Crashalot
1
@Crashalot 你可以检查这两个字符:‘和’。然后用'字符替换它们。 - micnguyen

8

我认为对于某些语言,smartQuotesTypesmartQuotesType不是一个好的实践。

在我们的应用程序中为每个文本输入设置这些属性:

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

创建一个分类来禁用这些“SMART”功能是没有意义的(错误):
- (UITextSmartDashesType)smartDashesType {
    return UITextSmartDashesTypeNo;
}
- (UITextSmartQuotesType)smartQuotesType {
    return UITextSmartQuotesTypeNo;
}
- (UITextSmartInsertDeleteType)smartInsertDeleteType {
    return UITextSmartInsertDeleteTypeNo;
}

因此,我尝试通过方法混编永久禁用这些功能:

#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

黑客总是能够轻易地突破私有方法的保护...

0

我在这方面遇到了问题 - 我经常使用我的iPad Pro通过Prompt和NX。 在设置中关闭“智能标点符号”即可解决问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接