Created
January 27, 2021 03:15
-
-
Save aaronlab/5cce6d96552a830f3ba6ee6d64df80b0 to your computer and use it in GitHub Desktop.
SwiftUI TextEditor Placeholder with NotificationCenter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ContentView: View { | |
@State var text = "Placeholder" | |
var body: some View { | |
TextEditor(text: self.$text) | |
.foregroundColor(self.text == "Placeholder" ? .gray : .primary) | |
.onAppear { | |
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: .main) { (noti) in | |
withAnimation { | |
if self.text == "Placeholder" { | |
self.text = "" | |
} | |
} | |
} | |
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { (noti) in | |
withAnimation { | |
if self.text == "" { | |
self.text = "Placeholder" | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment