Created
February 22, 2021 01:03
-
-
Save arpitdsoni/69a6b10b37944210750b4e4f2a60314f to your computer and use it in GitHub Desktop.
SwiftUI TextEditor with placeholder and border.
This file contains 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 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