简体   繁体   English

SwiftUI 文本字段颜色

[英]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.也可能值得考虑在UITextField周围进行表示,因为它更具可配置性。

Tested with Xcode 11.7 / iOS 13.7使用 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 备份

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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