Skip to content

Instantly share code, notes, and snippets.

@Koshimizu-Takehito
Forked from YusukeHosonuma/EnumPicker.swift
Last active September 23, 2024 00:42
Show Gist options
  • Save Koshimizu-Takehito/1508cc4915f5fd46607c5d4c965c4fe4 to your computer and use it in GitHub Desktop.
Save Koshimizu-Takehito/1508cc4915f5fd46607c5d4c965c4fe4 to your computer and use it in GitHub Desktop.
import SwiftUI
// Xcode: 16.0
// Swift: 6.0
struct EnumPicker<Enum>: View where Enum: CaseIterable & Hashable, Enum == Enum.AllCases.Element, Enum.AllCases: RandomAccessCollection {
@Binding var selection: Enum
var body: some View {
Picker(String(describing: Enum.self), selection: $selection) {
ForEach(Enum.allCases, id: \.self) { value in
Text("\(value)")
.tag(value)
}
}
}
}
extension Font.Leading: @retroactive CaseIterable {
public static let allCases: [Self] = [
.loose,
.standard,
.tight,
]
}
extension Font.Design: @retroactive CaseIterable {
public static let allCases: [Self] = [
.default,
.serif,
.rounded,
.monospaced,
]
}
#Preview(traits: .fixedLayout(width: 300, height: 300)) {
@Previewable @State var leading: Font.Leading = .standard
@Previewable @State var design: Font.Design = Font.Design.default
@Previewable @State var dynamicTypeSize: DynamicTypeSize = .medium
VStack {
EnumPicker(selection: $leading)
EnumPicker(selection: $design)
EnumPicker(selection: $dynamicTypeSize)
}
.dynamicTypeSize(dynamicTypeSize)
.padding()
}
@Koshimizu-Takehito
Copy link
Author

スクリーンショット 2024-09-23 9 38 38

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