本文介绍了SwiftUI文本字段颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么想法如何更改文本字段占位符的颜色,如所附图片吗?
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.
我想使其与图片一样.
非常感谢您的帮助
SecureField(String("Password"), text: $pass).padding(.all).border(Color.white).cornerRadius(3.0).padding(.horizontal)
推荐答案
在下面的演示中找到一些可能的方法.也可能值得考虑在 UITextField
周围使用可表示性,因为它更具可配置性.
Find below a demo of some possible approach. Also might worth considering representable around UITextField
as it is much more configurable.
通过Xcode 11.7/iOS 13.7测试
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)
}
}
这篇关于SwiftUI文本字段颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!