Skip to content

Instantly share code, notes, and snippets.

@arpitdsoni
Created February 22, 2021 01:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arpitdsoni/69a6b10b37944210750b4e4f2a60314f to your computer and use it in GitHub Desktop.
Save arpitdsoni/69a6b10b37944210750b4e4f2a60314f to your computer and use it in GitHub Desktop.
SwiftUI TextEditor with placeholder and border.
struct PlaceHolderTextEditor: View {
let placeholder: String
@Binding var text: String
var body: some View {
ZStack(alignment: Alignment(horizontal: .leading, vertical: .top)) {
if text.isEmpty {
Text(placeholder)
.foregroundColor(Color(.label))
.padding(.top, 10)
}
TextEditor(text: $text)
.opacity(text.isEmpty ? 0.7 : 1)
}
.padding([.leading, .trailing], 8)
.overlay(
RoundedRectangle(cornerRadius: 6)
.stroke(Color(.systemGray5), lineWidth: 1.0)
)
}
}
struct PlaceHolderTextEditor_Previews: PreviewProvider {
static var previews: some View {
StatefulPreviewWrapper("") { text in
PlaceHolderTextEditor(placeholder: "Placeholder", text: text)
}
.padding()
.frame(height: 300)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment