问题
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)
回答1:
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)
}
}
来源:https://stackoverflow.com/questions/63975796/swiftui-textfield-color