选择器标签更改为选定值 SwiftUI

Picker Label Changing to selected value SwiftUI

我在这里想要一个常量标签,但是我的选择器标签根据我的值发生变化 select。

我的代码:

Picker(
                        selection : $textBoxes[currentIndex].textFont,
                        label : Text("Font"),
                        content : {
                            Text("LuxuriousRoman-Regular").tag("LuxuriousRoman-Regular")
                            Text("Merriweather-Regular").tag("Merriweather-Regular")
                            Text("Neonderthaw-Regular").tag("Neonderthaw-Regular")
                            Text("OpenSansCondensed-Light").tag("OpenSansCondensed-Light")
                            Text("Pacifico").tag("Pacifico")
                            Text("PTSans-Regular").tag("PTSans-Regular")
                            Text("RobotoMono-VariableFont_wght").tag("RobotoMono-VariableFont_wght")
                            Text("SedgwickAve-Regular").tag("SedgwickAve-Regular")
                        }
                    ).pickerStyle(MenuPickerStyle())

在其菜单样式表单中使用 Picker 时,不会显示 label,而是使用所选项目,如您所见。

如果要使用标准标签,应使用 Menu 作为基本视图,然后使用 Picker 作为菜单的内容。例如,您可以在此处查看用法上的差异:

struct ContentView: View {
  enum DemoOption: String, CaseIterable {
    case one, two, three, four, five
  }
  
  @State private var pickerValue: DemoOption = .one
  @State private var menuValue: DemoOption = .two
  
  var body: some View {
    VStack {
      Picker("Picker Option", selection: $pickerValue) {
        ForEach(DemoOption.allCases, id: \.rawValue) { option in
          Text(option.rawValue).tag(option)
        }
      }
      .pickerStyle(.menu)
      
      Menu {
        Picker("Choose an option", selection: $menuValue) {
          ForEach(DemoOption.allCases, id: \.rawValue) { option in
            Text(option.rawValue).tag(option)
          }
        }
      } label: {
        Text("Choose an option")
      }
    }
  }
}