3.6_如何给TextField添加占位符

SwiftUI 的 TextField 支持占位符文本,就像 UITextField 一样 – 当 TextField 为空时在 TextField 中显示灰色文本。要么使用提示(”Enter your password”),要么显示一些示例数据。

要设置 placeholder,请将其作为 TextField 初始化设置的一部分传入,如下所示:

struct ContentView: View {
    @State private var emailAddress = ""
    var body: some View {
        TextField("test@gmail.com", text: $emailAddress)
            .padding()
            .textFieldStyle(RoundedBorderTextFieldStyle())
    }
}

效果预览: 3.6_textfield_placeholder 这将在 TextField 中显示占位文本 test@gmail.com,而 TextField 为空,但一旦用户在其中键入内容,占位文本就会消失。

Avatar
M X
Mobile, Front-End Developer

My research interests include swift developing, python developing and go developing.

Related

Next
Previous
comments powered by Disqus