How to add placeholder text to TextEditor in SwiftUI?

后端 未结 9 2147
逝去的感伤
逝去的感伤 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:57

    Until we have some API support, an option would be to use the binding string as placeholder and onTapGesture to remove it

    TextEditor(text: self.$note)
                    .padding(.top, 20)
                    .foregroundColor(self.note == placeholderString ? .gray : .primary)
                    .onTapGesture {
                        if self.note == placeholderString {
                            self.note = ""
                        }
                    }
    

提交回复
热议问题