Skip to content

Instantly share code, notes, and snippets.

@laurenchen0631
Created June 17, 2020 09:33
Show Gist options
  • Save laurenchen0631/523732dd0256a236911b74760c953cd1 to your computer and use it in GitHub Desktop.
Save laurenchen0631/523732dd0256a236911b74760c953cd1 to your computer and use it in GitHub Desktop.
Swiftui checkbox
// Based on https://medium.com/@mahmudahsan/how-to-create-checkbox-in-swiftui-ad08e285ab3d
import SwiftUI
struct CheckboxField<Label: View>: View {
@Binding var checked: Bool
let label: Label
let size: CGFloat
let color: Color
init(
label: Label,
size: CGFloat = 10,
color: Color = Theme.prompt,
checked: Binding<Bool>
) {
self.label = label
self.size = size
self.color = color
_checked = checked
}
var body: some View {
Button(
action: {
self.checked.toggle()
},
label: {
HStack(alignment: .center, spacing: 10) {
Image(
systemName: checked
? "checkmark.square"
: "square"
)
.resizable()
.scaledToFit()
.foregroundColor(color)
.frame(width: size, height: size)
label
}
}
)
}
}
struct CheckboxField_Previews: PreviewProvider {
static var previews: some View {
CheckboxField(
label: Text("This is Checkbox")
.foregroundColor(Theme.prompt)
.font(Font.system(size: 14)),
size: 14,
color: Theme.primary,
checked: .constant(false)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment