-
-
Save Koshimizu-Takehito/1508cc4915f5fd46607c5d4c965c4fe4 to your computer and use it in GitHub Desktop.
This file contains 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
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() | |
} |
Author
Koshimizu-Takehito
commented
Sep 23, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment