Created
September 22, 2024 12:49
-
-
Save YusukeHosonuma/d5192d7dbf3308a79fd186007354a06b 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 | |
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() | |
} |
Author
YusukeHosonuma
commented
Sep 22, 2024

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