Kyl*_*yle 15 uitextfield uitextview ios ios11
iOS 11在输入时添加了智能引号.在macOS中,我们可以NSTextView通过设置禁用智能引号:
textView.automaticQuoteSubstitutionEnabled = NO;
Run Code Online (Sandbox Code Playgroud)
既没有UITextField或UITextView似乎没有此属性或enabledTextCheckingTypes属性.如何在iOS 11上禁用智能引号?
Pau*_*w11 24
智能引号和其他功能(如智能破折号)通过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
}
Run Code Online (Sandbox Code Playgroud)
创建一个类别来禁用这些"SMART"功能毫无意义(bug):
- (UITextSmartDashesType)smartDashesType {
return UITextSmartDashesTypeNo;
}
- (UITextSmartQuotesType)smartQuotesType {
return UITextSmartQuotesTypeNo;
}
- (UITextSmartInsertDeleteType)smartInsertDeleteType {
return UITextSmartInsertDeleteTypeNo;
}
Run Code Online (Sandbox Code Playgroud)
所以我尝试通过方法调配永久禁用这些功能:
#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
Run Code Online (Sandbox Code Playgroud)
黑客私有方法总是像魅力一样......
| 归档时间: |
|
| 查看次数: |
5912 次 |
| 最近记录: |