SwiftUI Font.Design 选择器
SwiftUI Font.Design Picker
有人知道在选择器中循环 SwiftUI Font.Design 的 4 个枚举值以便为界面选择它们的有效方法吗?
可能性:
case `default`
case monospaced
case rounded
case serif
例如:
Picker("Font design", selection: $vm.fontDesign, content: {
ForEach(Font.Design, id:\.self) { design in
Text(design.rawValue).tag(design)
}
})
在 ViewModel 中:
@Published var fontDesign: Font.Design = .rounded
提前致谢!
您需要扩展 Font.Design
以符合 caseIterable
:
extension Font.Design: CaseIterable {
public static var allCases: [Font.Design]{
[.default, .monospaced, .rounded, .serif]
}
}
最终代码将如下所示(为了获取选择器的字符串值):
extension Font.Design: CaseIterable {
public static var allCases: [Font.Design] {
[.default, .monospaced, .rounded, .serif]
}
var design: String {
switch self {
case .default:
return "default"
case .monospaced:
return "monospaced"
case .rounded:
return "rounded"
case .serif:
return "serif"
default:
return "default"
}
}
}
查看:
Picker("fontDesign", selection: $vm.fontDesign, content: {
ForEach(Font.Design.allCases, id:\.self) { font in
Text("\(font.design)").tag(font)
}
})
有人知道在选择器中循环 SwiftUI Font.Design 的 4 个枚举值以便为界面选择它们的有效方法吗?
可能性:
case `default`
case monospaced
case rounded
case serif
例如:
Picker("Font design", selection: $vm.fontDesign, content: {
ForEach(Font.Design, id:\.self) { design in
Text(design.rawValue).tag(design)
}
})
在 ViewModel 中:
@Published var fontDesign: Font.Design = .rounded
提前致谢!
您需要扩展 Font.Design
以符合 caseIterable
:
extension Font.Design: CaseIterable {
public static var allCases: [Font.Design]{
[.default, .monospaced, .rounded, .serif]
}
}
最终代码将如下所示(为了获取选择器的字符串值):
extension Font.Design: CaseIterable {
public static var allCases: [Font.Design] {
[.default, .monospaced, .rounded, .serif]
}
var design: String {
switch self {
case .default:
return "default"
case .monospaced:
return "monospaced"
case .rounded:
return "rounded"
case .serif:
return "serif"
default:
return "default"
}
}
}
查看:
Picker("fontDesign", selection: $vm.fontDesign, content: {
ForEach(Font.Design.allCases, id:\.self) { font in
Text("\(font.design)").tag(font)
}
})