SwiftUI textfield color

拜拜、爱过 提交于 2020-12-13 19:01:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!