Skip to content

Instantly share code, notes, and snippets.

@YusukeHosonuma
Created September 22, 2024 12:49
Show Gist options
  • Save YusukeHosonuma/d5192d7dbf3308a79fd186007354a06b to your computer and use it in GitHub Desktop.
Save YusukeHosonuma/d5192d7dbf3308a79fd186007354a06b to your computer and use it in GitHub Desktop.
import SwiftUI
struct EnumPicker<Enum>: View where Enum: CaseIterable & Hashable, Enum.AllCases: RandomAccessCollection {
typealias Element = Enum.AllCases.Element
private var enumType: Enum.Type
@Binding private var selection: Element
init(_ enumType: Enum.Type, selection: Binding<Element>) {
self.enumType = enumType
_selection = selection
}
var body: some View {
Picker(String(describing: enumType), selection: $selection) {
ForEach(Enum.allCases, id: \.self) { value in
Text("\(value)")
.tag(value)
}
}
}
}
extension Font.Leading: CaseIterable {
public static var allCases: [Font.Leading] = [
.loose,
.standard,
.tight,
]
}
extension Font.Design: CaseIterable {
public static var allCases: [Font.Design] = [
.default,
.serif,
.rounded,
.monospaced,
]
}
#Preview(traits: .fixedLayout(width: 300, height: 300)) {
struct Preview: View {
@State var leading: Font.Leading = .standard
@State var design: Font.Design = .default
@State var dynamicTypeSize: DynamicTypeSize = .medium
var body: some View {
VStack {
EnumPicker(Font.Leading.self, selection: $leading)
EnumPicker(Font.Design.self, selection: $design)
EnumPicker(DynamicTypeSize.self, selection: $dynamicTypeSize)
}
.dynamicTypeSize(dynamicTypeSize)
.padding()
}
}
return Preview()
}
@YusukeHosonuma
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment