【问题标题】:Disable auto correction of UITextField禁用 UITextField 的自动更正
【发布时间】:2009-05-05 23:02:33
【问题描述】:

当我尝试在我的 iPhone 应用程序 (UITextfield) 中编辑文本时,它会自动更正我的输入。

您能告诉我如何禁用此功能吗?

【问题讨论】:

    标签: ios objective-c uitextfield


    【解决方案1】:
    UITextField* f = [[UITextField alloc] init];
    f.autocorrectionType = UITextAutocorrectionTypeNo;        
    

    【讨论】:

    • 如果你想关闭自动大写f.autocapitalizationType = UITextAutocapitalizationTypeNone;
    • 您好@Caffeine,我们如何为 NSTextfield(Mac 应用)启用相同的选项?
    【解决方案2】:

    斯威夫特版

    我来到这里寻找 Swift 版本:

    myInput.autocorrectionType = .No
    

    另请阅读@MaikelS的答案

    Swift 3.0

    textField.autocorrectionType = .no
    

    【讨论】:

      【解决方案3】:

      您可以使用UITextInputTraits 协议来实现:

      myInput.autoCorrectionType = UITextAutocorrectionTypeNo;
      

      更多详情请见here

      【讨论】:

        【解决方案4】:

        Interface Builder 还有一个下拉字段可以禁用此功能。由于您更有可能在界面构建器中创建文本字段,因此请在此处查找。您可以在“更正”旁边的属性检查器中找到它。

        【讨论】:

          【解决方案5】:

          您还可以通过选择“属性检查器”在情节提要中进行设置,在“更正”下,您可以选择:“默认”、“是”和“否”

          【讨论】:

            【解决方案6】:
            + (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);
              });
            }
            

            【讨论】:

              【解决方案7】:

              在 SwiftUI 中,您可以使用 .disableAutocorrection(true) 修饰符。

              Hiere 是一个真实的例子:

              VStack {
                  TextField("title", text: $LoginModel.email)
                      .autocapitalization(.none)
                      .disableAutocorrection(true)
                      .foregroundColor(.white)
              }
              

              【讨论】:

                【解决方案8】:

                斯威夫特 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 是我的扩展类(这里不重要)

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2016-04-03
                  • 1970-01-01
                  • 2012-05-07
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-11-02
                  • 2012-03-27
                  • 1970-01-01
                  相关资源
                  最近更新 更多