SwiftUI:是否可以关闭 TextField 的预测文本
SwiftUI: Is it possible to turn off predictive text for a TextField
我想在 SwiftUI 中为 TextField
关闭预测 text/autocorrect。看起来这在 UITextField
中是可能的:
Disable UITextField Predictive Text
我查看了 TextField
的 Apple 文档并进行了谷歌搜索,但找不到任何相关信息。
有没有人找到禁用 TextField 的预测 text/autocomplete 的方法?
谢谢!
似乎现在可以使用 Xcode 11 Beta 5。有一个新的修饰符可以禁用 TextField 的自动更正
func disableAutocorrection(_ disable: Bool?) -> some View
这应该有效:
.disableAutocorrection(true)
Xcode 12.3 Swift 5.3
如果您需要在多个 TextField
上禁用自动更正,或者确实添加其他修饰符,则创建一个自定义 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)
}
}
我想在 SwiftUI 中为 TextField
关闭预测 text/autocorrect。看起来这在 UITextField
中是可能的:
Disable UITextField Predictive Text
我查看了 TextField
的 Apple 文档并进行了谷歌搜索,但找不到任何相关信息。
有没有人找到禁用 TextField 的预测 text/autocomplete 的方法?
谢谢!
似乎现在可以使用 Xcode 11 Beta 5。有一个新的修饰符可以禁用 TextField 的自动更正
func disableAutocorrection(_ disable: Bool?) -> some View
这应该有效:
.disableAutocorrection(true)
Xcode 12.3 Swift 5.3
如果您需要在多个 TextField
上禁用自动更正,或者确实添加其他修饰符,则创建一个自定义 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)
}
}