【问题标题】:SwiftUI: Is it possible to turn off predictive text for a TextFieldSwiftUI:是否可以关闭 TextField 的预测文本
【发布时间】:2019-07-26 05:08:22
【问题描述】:

我想在 SwiftUI 中为 TextField 关闭预测文本/自动更正。看起来这在UITextField 中是可能的: Disable UITextField Predictive Text

我检查了 Apple 文档中的 TextField 并用 Google 搜索,但找不到任何相关信息。

有没有人找到一种方法来禁用文本字段的预测文本/自动完成功能?

谢谢!

【问题讨论】:

    标签: textfield swiftui


    【解决方案1】:

    现在似乎可以使用 Xcode 11 Beta 5。有一个新的修饰符可以禁用 TextField 上的自动更正

    func disableAutocorrection(_ disable: Bool?) -> some View
    

    https://developer.apple.com/documentation/swiftui/textfield/3367734-disableautocorrection?changes=latest_beta

    【讨论】:

      【解决方案2】:

      这应该可行:

      .disableAutocorrection(true)
      

      【讨论】:

        【解决方案3】:

        Xcode 12.3 Swift 5.3

        如果您需要在多个TextFields 上禁用自动更正,或者确实添加其他修饰符,则创建自定义 TextField:

        struct TextFieldCustom: View {
            
            let title: String
            let text: Binding<String>
            
            init(_ title: String, text: Binding<String>) {
                self.title = title
                self.text = text
            }
            
            var body: some View {
                TextField(title, text: text)
                    .disableAutocorrection(true)
                    // add any other modifiers that you want
            }
        }
        

        示例用法:

        Form {
            Section(header: Text("Details")) {
                TextFieldCustom("Field1", text: $field1)
                TextFieldCustom("Feild2", text: $field2)
                TextFieldCustom("Field3", text: $field3)
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2020-05-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-06
          • 1970-01-01
          • 2010-11-29
          • 2013-02-22
          相关资源
          最近更新 更多