简体   繁体   中英

SwiftUI textfield color

any idea how to change the color of the textfield placeholder like the attached picture?

I can't find the way to color the text "email" in white, it always stay in grey.

i want to make it same like the picture.

thanks a lot for the help

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



我想复制这个

Find below a demo of some possible approach. Also might worth considering representable around UITextField as it is much more configurable.

Tested with 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)

    }
}

backup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM