4

I saw this question but it doesn't work in SwiftUI.

Changing text color of datepicker

I tried .forgroundColor and .accentColor but they don't change the texts' color.

pkamb
  • 30,595
  • 22
  • 143
  • 179
BabakHSL
  • 552
  • 5
  • 16

7 Answers7

12

I just simple set the accentColor and it works.

@State private var date = Date()
DatePicker("Select a date",
           selection: $date, in: ...Date(), 
           displayedComponents: .date)
        .labelsHidden()
        .accentColor(.orange)
Hoang Anh Tuan
  • 214
  • 3
  • 8
  • This should be the accepted answer - duplicate post with accepted answer -https://stackoverflow.com/a/64823381/5632572 – Michael Ellis Mar 08 '22 at 16:20
  • For the `.compact` date picker style this accent color is shown when active, but the text is black before the control is tapped. – pkamb Apr 21 '22 at 19:59
8

try this:

var body: some View {
    Group {
        
        DatePicker(selection: $selected) {
            Text("Date")
        }
    
    .colorInvert()
        .colorMultiply(Color.blue)
    }
}

enter image description here

pkamb
  • 30,595
  • 22
  • 143
  • 179
Chris
  • 6,342
  • 3
  • 15
  • 34
  • Please, be worry that it could change the functionality of some controls. try to apply the same technic on Toggle and it stops working ... – user3441734 Mar 06 '20 at 11:22
  • 1
    @UserWithWeiredNumber. nobody said it will work on all controls. nobody said - and each developer knows - Apple can change things and then we have to change some things too. But some people only see "worries". He asked for changing the color of the datepicker - he searches for a solution NOW - not in 100 years. that is what my solution provides. nothing more nothing less. – Chris Mar 06 '20 at 11:40
  • I already choose your answer as valuable :-), it is still a good idea to inform the people. Especially because the Toggle could be deeper in your UI. You can try it yourself ... To be warned is better – user3441734 Mar 06 '20 at 11:57
  • i cannot see who flagged my answers... ;) – Chris Mar 06 '20 at 12:00
7

Using .colorInvert() and .colorMultiply(.white) will change the text color to white but won't work when the device is in Dark Mode. Instead, try this:

DatePicker("Date", selection: $selection)
    .colorScheme(.dark) // or .light to get black text
Pranav Wadhwa
  • 7,312
  • 6
  • 34
  • 57
  • 1
    This is the best answer currently, but hope that Apple will give us the way to change text color in the future – Adelmaer Oct 06 '21 at 18:24
2

you can do this to make the text appear white in light or dark mode if you use a background color

struct PickerDate: View {
    @Environment(\.colorScheme) var colorScheme
    @State var date: Date
    var body: some View {
        VStack {
            DatePicker("Record Date", selection: $date, in: ...Date(), displayedComponents: .date)
                .labelsHidden()
                .colorMultiply(colorScheme == .dark ? .black : .white)
                .colorInvert()
            }
        }
    }
}
pkamb
  • 30,595
  • 22
  • 143
  • 179
0
DatePicker("DatePicker", selection: self.$date, displayedComponents: .hourAndMinute)
              .labelsHidden()
              .colorMultiply(Color.white)
              .colorInvert()
  • 3
    From Review:  Hi, please don't answer just with source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp May 29 '20 at 08:44
0

I use extension for customized text color for both of light mode and dark mode.

extension View {
    @ViewBuilder func changeTextColor(_ color: Color) -> some View {
        if UITraitCollection.current.userInterfaceStyle == .light {
            self.colorInvert().colorMultiply(color)
        } else {
            self.colorMultiply(color)
        }
    }
}

and sample is as below.

DatePicker("Date", selection: $selection)
    .labelsHidden()
    .changeTextColor(.green)
LinusGeffarth
  • 24,641
  • 28
  • 109
  • 162
dskf7
  • 1
0

I found applying this modifier to the DatePicker to be cleaner:

Forces the color to white

 .environment(\.colorScheme, .dark)

Forces the color to dark

 .environment(\.colorScheme, .light)
tennis779
  • 318
  • 2
  • 6
  • 18