How to add placeholder text to TextEditor in SwiftUI?

后端 未结 9 2165
逝去的感伤
逝去的感伤 2021-02-14 01:35

When using SwiftUI\'s new TextEditor, you can modify its content directly using a @State. However, I haven\'t see a way to add a placeholder text to it. Is it doable right now?<

9条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-14 01:55

    You can use a ZStack with a disabled TextEditor containing your placeholder text behind. For example:

    ZStack {
        if self.content.isEmpty {
                TextEditor(text:$placeholderText)
                    .font(.body)
                    .foregroundColor(.gray)
                    .disabled(true)
                    .padding()
        }
        TextEditor(text: $content)
            .font(.body)
            .opacity(self.content.isEmpty ? 0.25 : 1)
            .padding()
    }
    

提交回复
热议问题