ManorBoy
ManorBoy

Reputation: 99

Picker Text color in DarkMode

How do set the text in a Picker control to a light color for iOS13 darkmode

To support DARKMODE on IOS13 you need to set the text color to a colour that the system can change. But on the inspector, there is no ability to set the Picker text to any color.

There must be a simple way to do this but I cannot find it. Using Attributed text is NOT the solution.

Upvotes: 2

Views: 1368

Answers (3)

Vidu
Vidu

Reputation: 461

I have used "UIColor.label" for "NSAttributedString.Key.foregroundColor".

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        if #available(iOS 13.0, *) {
            return NSAttributedString(string: "ABC", attributes: [NSAttributedString.Key.foregroundColor: UIColor.label])
        } else {
            // Fallback on earlier versions
            return NSAttributedString(string: "ABC", attributes: [NSAttributedString.Key.foregroundColor: UIColor.black])
        }
    }

Upvotes: 1

Victor
Victor

Reputation: 9

In storyboard, select the Attribute Inspector / View / Tint / Label Color :

enter image description here

That will make the font:

  • white, in dark mode.
  • black, in light mode.

Upvotes: 0

ManorBoy
ManorBoy

Reputation: 99

Changing the Tint colour made no difference but I noticed that the font was different to everything else - that made me suspicious that the font was being set somewhere and I have found the answer. In the picker-view functions that are part of the delegate support for picker controls, there is a font setting that includes its colour.

Upvotes: 0

Related Questions