Post

Replies

Boosts

Views

Activity

Reply to How to add placeholder text to TextEditor in SwiftUI?
As I know, this is the best way to add a placeholder text to TextEditor in SwiftUI struct ContentView: View { 		@State var text = "Type here" 		 		var body: some View { 				TextEditor(text: self.$text) 						// make the color of the placeholder gray 						.foregroundColor(self.text == "Type here" ? .gray : .primary) 						 						.onAppear { 								// remove the placeholder text when keyboard appears 								NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: .main) { (noti) in 										withAnimation { 												if self.text == "Type here" { 														self.text = "" 												} 										} 								} 								 								// put back the placeholder text if the user dismisses the keyboard without adding any text 								NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { (noti) in 										withAnimation { 												if self.text == "" { 														self.text = "Type here" 												} 										} 								} 						} 		} }
Sep ’20