2

When using a Picker on a Form in SwiftUI, if the Picker is disabled, the selected item's text is light gray. It appears a little too light for readability. How can this be changed? Disabled control

Enabled Control There is no font-colour option, especially for disabled state, identified in Apple's documentation.

    var strengths = ["Mild", "Medium", "Mature"]

    @State private var selectedStrength = 0

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker(selection: $selectedStrength, label: Text("Strength")) {
                        ForEach(0 ..< strengths.count) {
                            Text(self.strengths[$0])

                        }
                    }
                    .disabled(true)
                }
            }.navigationBarTitle("Select your cheese")

        }
    }
}

I would like to change the font color so that it can be made more readable.

Justin Ngan
  • 1,050
  • 12
  • 20

1 Answers1

-2

Get the index in that ForEach and check it with the observables.selection. Then change the color for text if needed:

...
ForEach (0 ..< observables.Intact.endIndex, id: \.self) { index in
    Text(self.observables.Intact[$0]).tag($0)
        .foregroundColor(self.observables.selection == index ? .red : .blue )
}
...

Picker

You can combine selection && editMode if you need to detect it for setting colors.

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • Thanks for the reply, but there's a couple of important points that you've missed in my question: 1) The Picker appears in a Form, and 2) The text color is the color when the Picker is disabled. Number 1 means that the Picker is not of the wheelPicker style, at least not as the default Picker style of a form. Number 2 means that the text is not the 'normal' state, that is, the text of a disabled control is lightened. I want to stop that or lessen it. What is causing the lightening? Is it text color, a modifier on a row, how can I change it? – Justin Ngan Sep 14 '19 at 09:43
  • 1) I used your code to reproduce the situation, please update it if it's different with the real one. 2) as I said, `selection` && `editMode` to modify *disabled* stated. – Mojtaba Hosseini Sep 14 '19 at 09:48
  • Sorry, selection && editMode don't cut it ! You know this is within SwiftUI right? – Justin Ngan Sep 14 '19 at 10:00
  • You changed the code **A lot!**. So this is a completely different question. I thing you should revert the code back and ask a new question my friend. – Mojtaba Hosseini Sep 14 '19 at 10:13