Created
July 11, 2021 14:15
-
-
Save floydnoel/8c4411d19ffd85e83d748e539f41ab9b to your computer and use it in GitHub Desktop.
A TextEditor component with a placeholder
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
// | |
// NoteField.swift | |
// | |
// Created by Floyd Noel on 7/11/21. | |
// | |
import SwiftUI | |
struct NoteField: View { | |
var label: String | |
@Binding var contents: String | |
var body: some View { | |
ZStack { | |
if (contents.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).isEmpty) { | |
VStack { | |
HStack { | |
Text(label).opacity(0.25).padding(EdgeInsets(top: 8, leading: 0, bottom: 0, trailing: 0)) | |
Spacer() | |
} | |
Spacer() | |
} | |
} | |
TextEditor(text: $contents) | |
.padding(EdgeInsets(top: 0, leading: -4, bottom: 0, trailing: 0)) | |
.frame(minWidth: 0, idealWidth: 100, maxWidth: .infinity, minHeight: 100, idealHeight: 200, maxHeight: .infinity, alignment: .center) | |
.accessibilityLabel(label) | |
} | |
} | |
} | |
struct NoteField_Previews: PreviewProvider { | |
@State static var contents: String = "" | |
static var previews: some View { | |
Form { | |
NoteField(label: "Placeholder", contents: $contents) | |
}.preferredColorScheme(.dark) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment