【发布时间】:2009-05-05 23:02:33
【问题描述】:
当我尝试在我的 iPhone 应用程序 (UITextfield) 中编辑文本时,它会自动更正我的输入。
您能告诉我如何禁用此功能吗?
【问题讨论】:
标签: ios objective-c uitextfield
当我尝试在我的 iPhone 应用程序 (UITextfield) 中编辑文本时,它会自动更正我的输入。
您能告诉我如何禁用此功能吗?
【问题讨论】:
标签: ios objective-c uitextfield
UITextField* f = [[UITextField alloc] init];
f.autocorrectionType = UITextAutocorrectionTypeNo;
【讨论】:
f.autocapitalizationType = UITextAutocapitalizationTypeNone;
我来到这里寻找 Swift 版本:
myInput.autocorrectionType = .No
另请阅读@MaikelS的答案
Swift 3.0
textField.autocorrectionType = .no
【讨论】:
【讨论】:
Interface Builder 还有一个下拉字段可以禁用此功能。由于您更有可能在界面构建器中创建文本字段,因此请在此处查找。您可以在“更正”旁边的属性检查器中找到它。
【讨论】:
+ (void)disableAutoCorrectionsForTextfieldsAndTextViewGlobally {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
struct objc_method_description autocorrectionTypeMethodDescription =
protocol_getMethodDescription(@protocol(UITextInputTraits),
@selector(autocorrectionType), NO, YES);
IMP noAutocorrectionTypeIMP_TEXT_FIELD =
imp_implementationWithBlock(^(UITextField *_self) {
return UITextAutocorrectionTypeNo;
});
IMP noAutocorrectionTypeIMP_TEXT_VIEW =
imp_implementationWithBlock(^(UITextView *_self) {
return UITextAutocorrectionTypeNo;
});
class_replaceMethod([UITextField class], @selector(autocorrectionType),
noAutocorrectionTypeIMP_TEXT_FIELD,
autocorrectionTypeMethodDescription.types);
class_replaceMethod([UITextView class], @selector(autocorrectionType),
noAutocorrectionTypeIMP_TEXT_VIEW,
autocorrectionTypeMethodDescription.types);
});
}
【讨论】:
在 SwiftUI 中,您可以使用 .disableAutocorrection(true) 修饰符。
Hiere 是一个真实的例子:
VStack {
TextField("title", text: $LoginModel.email)
.autocapitalization(.none)
.disableAutocorrection(true)
.foregroundColor(.white)
}
【讨论】:
斯威夫特 5:
这就是我在项目中实现电子邮件地址字段的方式
private let emailTextField: UITextField = {
let tf = CustomTextField(placeholder: "Email address")
tf.keyboardType = .emailAddress
tf.autocorrectionType = .no //disable auto correction
tf.autocapitalizationType = .none //disable default capitalization
return tf
}()
CustomTextField 是我的扩展类(这里不重要)
【讨论】: