iOS 11 – 禁用智能引号
iOS 11在输入时添加了智能引号。 在macOS中,我们可以通过设置来禁用NSTextView
上的智能引号:
textView.automaticQuoteSubstitutionEnabled = NO;
UITextField
或UITextView
似乎都没有此属性或enabledTextCheckingTypes属性。 如何在iOS 11上禁用智能引号?
智能引号和其他function(如智能破折号)通过UITextField
协议控制, UITextInputTraits
协议由UITextField
和UITextView
。
具体来说, smartQuotesType
属性可以设置为.default
, .yes
或.no
。 目前没有关于这些值的进一步文档,但.yes
和.no
是不言自明的。 我对.default
猜测是系统将使用textContentType
和isSecureTextEntry
等属性来确定适当的行为。
例如,文本内容类型的电子邮件,密码或URL可能默认禁用智能引号,而作业标题可能默认启用。 我想安全文本输入字段也会默认禁用智能。
为输入视图设置适当的文本内容类型可以显着改善用户体验,强烈建议使用。
我认为smartQuotesType
和smartQuotesType
不是某些语言的好习惯。
为我们的应用中的每个文本输入设置以下属性:
if (@available(iOS 11.0, *)) { textView.smartDashesType = UITextSmartDashesTypeNo; textView.smartQuotesType = UITextSmartQuotesTypeNo; textView.smartInsertDeleteType = UITextSmartInsertDeleteTypeNo; } else { // Fallback on earlier versions }
创建一个类别来禁用这些“SMART”function毫无意义(bug):
- (UITextSmartDashesType)smartDashesType { return UITextSmartDashesTypeNo; } - (UITextSmartQuotesType)smartQuotesType { return UITextSmartQuotesTypeNo; } - (UITextSmartInsertDeleteType)smartInsertDeleteType { return UITextSmartInsertDeleteTypeNo; }
所以我尝试通过方法调配永久禁用这些function:
#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
黑客私有方法总是像魅力一样……
我有一个问题 – 我经常通过我的iPad Pro使用Prompt和NX。 在设置工作中关闭“智能标点符号”。