SwiftUI 文本字段颜色

Dam*_*zzi 2 xcode colors textfield swiftui xcode12

知道如何更改文本字段占位符的颜色(如附图所示)吗?

我找不到将文本“电子邮件”着色为白色的方法,它始终保持灰色。

我想让它像图片一样。

非常感谢您的帮助

SecureField(String("Password"), text: $pass).padding(.all).border(Color.white).cornerRadius(3.0).padding(.horizontal)



Run Code Online (Sandbox Code Playgroud)

我想复制这个

Asp*_*eri 5

下面是一些可能方法的演示。也可能值得考虑周围的代表性,UITextField因为它更具可配置性。

使用 Xcode 11.7 / iOS 13.7 进行测试

演示

struct PlaceholderTextFieldStyle: TextFieldStyle {
    let placeholder: String
    @Binding var text: String

    init(_ placeholder: String, text: Binding<String>) {
        self.placeholder = placeholder
        self._text = text
    }

    func _body(configuration: TextField<Self._Label>) -> some View {
        ZStack {
            if text.isEmpty {
                Text(placeholder)
            }
            configuration
        }.foregroundColor(.white)
    }
}

struct DemoView: View {
    @State private var pass = ""

    var body: some View {
        SecureField("", text: $pass)
        .textFieldStyle(PlaceholderTextFieldStyle("Password", text: $pass))
        .padding(.all).border(Color.white).cornerRadius(3.0).padding(.horizontal)
        .padding()
        .background(Color.blue)

    }
}
Run Code Online (Sandbox Code Playgroud)