SwiftUI如何更改TextField的占位符颜色?

【字号: 日期:2024-04-14浏览:43作者:雯心
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解决SwiftUI如何更改TextField的占位符颜色??

目前还没有api。 :

ZStack自己使用和实现占位符:

var body: some View { ZStack(alignment: .leading) {if text.isEmpty { Text('Placeholder').foregroundColor(.red) }TextField('', text: $text) }}

现在,您可以像老板一样使用任何类型的编辑。

-自定义TextField

您总是可以创建自己的custom,View以在所有地方使用:

struct CustomTextField: View { var placeholder: Text @Binding var text: String var editingChanged: (Bool)->() = { _ in } var commit: ()->() = { } var body: some View {ZStack(alignment: .leading) { if text.isEmpty { placeholder } TextField('', text: $text, onEditingChanged: editingChanged, onCommit: commit)} }}

用法 :

struct ContentView: View { @State var text = '' var body: some View {CustomTextField( placeholder: Text('placeholder').foregroundColor(.red), text: $text) }}解决方法

我想更改TextField的占位符颜色,但是找不到它的方法。

我尝试设置foregroundColor和accentColor,但它不会更改占位符的颜色。

这是代码:

TextField('Placeholder',$text) .foregroundColor(Color.red) .accentColor(Color.green)

也许还没有API?

相关文章: