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的占位符颜色,但是找不到它的方法。

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

这是代码:

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

也许还没有API?

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...