Skip to content

Instantly share code, notes, and snippets.

@floydnoel
Created July 11, 2021 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floydnoel/8c4411d19ffd85e83d748e539f41ab9b to your computer and use it in GitHub Desktop.
Save floydnoel/8c4411d19ffd85e83d748e539f41ab9b to your computer and use it in GitHub Desktop.
A TextEditor component with a placeholder
//
// 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