代码之家  ›  专栏  ›  技术社区  ›  c00000fd

如何为macOS禁用TextField上的自动填充?

  •  0
  • c00000fd  · 技术社区  · 2 年前

    我正在执行以下操作以禁用我的macOS应用程序中的所有自动填充:

                Text("ID:")
                TextField("", text: bindingCurrentID)
                    .disableAutocorrection(true)
    

    但它没有任何作用。如果我开始在文本字段中键入空格(在已经存在的文本之后),操作系统会自动在末尾添加一个句点。

    如何禁用它?或者删除所有自动完成的typex,只让用户键入的内容进入该字段?

    0 回复  |  直到 2 年前
        1
  •  0
  •   bpisano    2 年前

    .disableAutocorrection() deprecated .你应该使用 .autocorrectionDisabled() 相反请参阅文档 here

    struct ContentView: View {
        @State private var text: String = ""
        
        var body: some View {
            TextField("Email", text: $text)
                .autocorrectionDisabled() // prevents automatic correction from the system.
        }
    }
    
        2
  •  0
  •   ChrisR    2 年前

    一种可能的方法:

                TextField("", text: $input)
                    .autocorrectionDisabled(true)
                
                    .onChange(of: input) { _ in
                        input = input.replacingOccurrences(of: ".", with: "  ")
                    }