SwiftUI - 如何更改文本字段的文本颜色?
SwiftUI - How do I change the text color of a Textfield?
我正在尝试更改 SwiftUI 中文本字段(用户编写的文本)的文本颜色。我正在尝试使用 foregroundColor 但它会更改占位符的文本颜色。
如何在 SwiftUI 中执行此操作?
TextField($variable, placeholder: Text("a text"))
.foregroundColor(.green)
.foregroundColor
实际上确实改变了 TextField
的文本颜色,但只有当它有一个默认值时,例如这样才能正常工作:
TextField(.constant("Hello World"), placeholder: Text("Type here..."))
.foregroundColor(.green)
但是一旦您删除整个文本,文本字段不仅会失去颜色,还会失去其他修饰符,例如对齐方式。这可能是当前版本中的错误,因此我将提交错误报告。
更新:此问题已通过 Xcode 11 beta 3.
修复
应更改文本颜色以使用文本字段 .foreground color
的 属性
为 Xcode Version 11.1 (11A1027)
更新
SwiftUI
TextField("Email", text: $email)
.textContentType(.emailAddress)
.padding(.init(top: 0, leading: 15, bottom:0 , trailing: 15))
.foregroundColor(.blue)
.textFieldStyle(RoundedBorderTextFieldStyle.init())
使用background()
设置背景颜色,foreground()
设置输入文本颜色
构造 PlaygroundTestView:视图 {
@State var searchText = ""
var body: some View {
TextField("Search", text: $searchText)
.font(Font.system(size: 14))
.padding()
.background(RoundedRectangle(cornerRadius: 50).fill(Color.white))
.foregroundColor(.black)
.padding()
.offset(x:8, y: 10)
}
}
foregroundColor does it.
Text("TEXT")
.font(.footnote)
.bold()
.foregroundColor(Color("ColorAccentWhite"))
.background(Color("ColorGrayTwo"))
.cornerRadius(2)
我正在尝试更改 SwiftUI 中文本字段(用户编写的文本)的文本颜色。我正在尝试使用 foregroundColor 但它会更改占位符的文本颜色。
如何在 SwiftUI 中执行此操作?
TextField($variable, placeholder: Text("a text"))
.foregroundColor(.green)
.foregroundColor
实际上确实改变了 TextField
的文本颜色,但只有当它有一个默认值时,例如这样才能正常工作:
TextField(.constant("Hello World"), placeholder: Text("Type here..."))
.foregroundColor(.green)
但是一旦您删除整个文本,文本字段不仅会失去颜色,还会失去其他修饰符,例如对齐方式。这可能是当前版本中的错误,因此我将提交错误报告。
更新:此问题已通过 Xcode 11 beta 3.
修复应更改文本颜色以使用文本字段 .foreground color
的 属性
为 Xcode Version 11.1 (11A1027)
SwiftUI
TextField("Email", text: $email)
.textContentType(.emailAddress)
.padding(.init(top: 0, leading: 15, bottom:0 , trailing: 15))
.foregroundColor(.blue)
.textFieldStyle(RoundedBorderTextFieldStyle.init())
使用background()
设置背景颜色,foreground()
设置输入文本颜色
构造 PlaygroundTestView:视图 {
@State var searchText = ""
var body: some View {
TextField("Search", text: $searchText)
.font(Font.system(size: 14))
.padding()
.background(RoundedRectangle(cornerRadius: 50).fill(Color.white))
.foregroundColor(.black)
.padding()
.offset(x:8, y: 10)
}
}
foregroundColor does it.
Text("TEXT")
.font(.footnote)
.bold()
.foregroundColor(Color("ColorAccentWhite"))
.background(Color("ColorGrayTwo"))
.cornerRadius(2)