【问题标题】:SwiftUI textfield colorSwiftUI 文本字段颜色
【发布时间】:2021-01-06 13:49:27
【问题描述】:
知道如何更改文本字段占位符的颜色,如所附图片吗?
我找不到将文本“电子邮件”着色为白色的方法,它总是保持灰色。
我想让它和图片一样。
非常感谢您的帮助
SecureField(String("Password"), text: $pass).padding(.all).border(Color.white).cornerRadius(3.0).padding(.horizontal)
【问题讨论】:
标签:
xcode
colors
swiftui
textfield
xcode12
【解决方案1】:
在下面找到一些可能方法的演示。也可能值得考虑在 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)
}
}