How to change DatePicker's texts color in SwiftUI? - datepicker

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.

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

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)

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

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)

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()
}
}
}
}

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)

DatePicker("DatePicker", selection: self.$date, displayedComponents: .hourAndMinute)
.labelsHidden()
.colorMultiply(Color.white)
.colorInvert()

Related

How to change Selection colour(AM,PM) and time of GraphicalDatePickerStyle Date picker Swiftui

How to Change GraphicalDatePickerStyle Date picker text color and Selection Colour:-
Code:-
DatePicker("Time",selection: dateProxy,displayedComponents: [.hourAndMinute])
.datePickerStyle(GraphicalDatePickerStyle())
.accentColor(.white)
.labelsHidden()
Output:-
But I want to achieve DatePicker given below, is it possible in GraphicalDatePickerStyle Style or not?
Try to Achieve:-
Seems like you are looking for the dark mode picker! Just apply the dark colorScheme to the component:
DatePicker("Time", selection: $date, displayedComponents: [.hourAndMinute])
.datePickerStyle(GraphicalDatePickerStyle())
.background(Color.black)
.environment(\.colorScheme, .dark) // <- This modifier
Result:
🌈 Apply color
Also, you can apply the colorMultiply modifier for coloring the picker in a pleasant way:
Demo Full Code
struct ContentView: View {
#State private var date = Date()
var colors: [Color] = [.white, .gray, .red, .green, .blue, .orange, .yellow, .pink, .purple] // <- You can use any color! This is just for demonstrate
var body: some View {
VStack {
ForEach(colors, id:\.self) { color in
DatePicker("Time", selection: $date, displayedComponents: [.hourAndMinute])
.datePickerStyle(GraphicalDatePickerStyle())
.background(Color.black.grayscale(1))
.colorMultiply(color) // <- This line does the trick
.environment(\.colorScheme, .dark) // <- Don't Forget this
}
}
}
}
This works for macOS 11 and iOS 14 (Xcode 12 beta 3)...
1.Take the Label out of the DatePicker - I've left it commented out here so its obvious to readers that the Label should be taken out of the DatePicker View.
Place the DatePicker in an HStack (or other ViewBuilder.
Add a Label for the DatePicker in the HStack.
Use the .colorMultiplier view modifier to change the color of the DatePicker date and time "button".
In this particular example I am also changing the colour of the date and time "button" depending on whether the binding value in the date picker is nil.
let now = Date()
HStack {
Text("Date")
DatePicker(selection: Binding($observedObject.date, replacingNilWith: now)) {
// Text("Date")
// .foregroundColor(Color(.label))
}
.colorMultiply(observedObject.date == nil ? Color(.placeholderText) : Color(.label))
}
Also I'm using an extension to Binding in this example which is something I use throughout my core data projects.
public extension Binding where Value: Equatable {
init(_ source: Binding<Value?>, replacingNilWith nilProxy: Value) {
self.init(
get: { source.wrappedValue ?? nilProxy },
set: { newValue in
if newValue == nilProxy {
source.wrappedValue = nil
}
else {
source.wrappedValue = newValue
}
})
}
}
As always full credit to Alan Quatermain for this extension to Binding.
Closest I got:
Group {
DatePicker("Time",selection: $dateProxy, displayedComponents: [.hourAndMinute])
.datePickerStyle(GraphicalDatePickerStyle())
.accentColor(.black)
.colorInvert()
.labelsHidden()
.padding()
}
.background(Color.black.opacity(0.8))

SwiftUI Text foreground color turns the background color

It seems that in order to change the font/text color it is recommended to use the foreground property. When I set that color to black, it also changes the capsule color 🤦. How can I just specify the text to change color?
import SwiftUI
struct CapsuleButtonFilled: View {
// MARK: - PROPERTIES
var backgroundColor: Color
// MARK:
var body: some View {
Button(action: {}, label: {
Text("The Solid")
.background(backgroundColor)
.foregroundColor(Color.black)
.padding()
})
.background(
Capsule(style: .circular)
.background(backgroundColor)
)
}
}
struct CapsuleButtonFilled_Previews: PreviewProvider {
static var previews: some View {
CapsuleButtonFilled(backgroundColor: .orange)
.previewLayout(.sizeThatFits)
}
}
When I set that color to black, it also changes the capsule color 🤦.
The default fill color of Capsule is black. For example, here's a plain Capsule with nothing applied to it.
struct CapsuleButtonFilled: View {
var body: some View {
Capsule(style: .circular)
}
}
struct CapsuleButtonFilled_Previews: PreviewProvider {
static var previews: some View {
CapsuleButtonFilled()
.previewLayout(.fixed(width: 400, height: 100))
}
}
Result:
Let's say you set Color.green for the text color.
Button(action: {}, label: {
Text("The Solid")
.foregroundColor(Color.green) /// green!
.background(backgroundColor)
.padding()
})
.background(
Capsule(style: .circular)
.background(backgroundColor)
)
This is the result:
The .foregroundColor() applied to the Text doesn't affect the capsule.
Anyway, I assume that this is the result you want?
You are using the wrong modifier to apply color to the capsule. Use .fill() instead of .background().
Button(action: {}, label: {
Text("The Solid")
.foregroundColor(Color.black)
.background(backgroundColor)
.padding()
})
.background(
Capsule(style: .circular)
.fill(backgroundColor)
)

How can I change DatePicker font weight in SwiftUI

I've tried below code. But .font function does not change anything. I want to the date more darker. How can I do that?
struct TestView: View {
#State private var date = Date()
var body: some View {
HStack {
DatePicker("", selection: $date, in: ...Date(), displayedComponents: .date)
.padding()
.labelsHidden()
.accentColor(.red)
.font(Font.body.weight(.bold))
Spacer()
}
}
}
While you can't specifically change the text for the DatePicker, you can use and HStack to accomplish the desired outcome.
HStack {
Text("End Date")
.font(.custom((Fonts.gse), size: 18))
.fontWeight(.medium)
Spacer()
DatePicker("", selection: self.$endingDate, in: self.dateRange, displayedComponents: [.date, .hourAndMinute])
}

SwiftUI 2.0 Change DatePicker background

Is there anyway to change the background color of the DatePicker view? I have the following Picker:
DatePicker(selection: $form.start, in: Date()..., displayedComponents: .date) {}
.padding(.vertical, 6)
.labelsHidden()
.accentColor(.black)
But the picker has that grayish tint around the date (See image below). I just want the entire background to be white. I tried .background(Color.white) but that doesn't do anything. How can I make the entire background white?
I discovered that you can do this by initialising it as follows:
init() {
UIDatePicker.appearance().backgroundColor = UIColor.init(.white) // changes bg color
UIDatePicker.appearance().tintColor = UIColor.init(.white) // changes font color
}
I don't agree with changing the default DatePicker, specially with this kind of 🔨, as you risk break future OS updates of it plus your/your designers vision of it is probably not as intuitive/accessible as Apple vision.
Still, if you are required to do it as I had to, here is how to do it while developing with a min target of iOS 14.
struct DatePickerView: View {
#State private var selectedDate = Date()
let dateFormatter = DateFormatter()
var body: some View {
HStack {
Text(dateFormatter.string(from: selectedDate))
.overlay {
DatePicker(
"",
selection: $selectedDate,
displayedComponents: .date
)
.blendMode(.destinationOver)
}
}
}
}
You can do this as follows:
dateTimePicker.setValue(UIColor.white, forKey: "backgroundColor")

Selected tab image for a TabView's TabItem is always blue in SwiftUI

I'm playing around with a TabView in SwiftUI and I'm able to set the image of a tab that is not currently selected, however, setting the image for a selected tab doesn't seem to be working. All I see is a blue circle instead of a black one.
Here is my code:
import SwiftUI
struct MainView: View {
#State private var selected = 0
var body: some View {
TabView(selection: $selected){
Text("First View")
.tabItem {
Image(self.selected == 0 ? "eclipse-tab-icon-black" : "eclipse-tab-icon-grey")
.renderingMode(.original)
}.tag(0)
Text("Second View")
.tabItem {
Image(systemName: "2.circle")
}.tag(1)
Text("Third View")
.tabItem {
Image(systemName: "3.circle")
}.tag(1)
}
}
}
struct MainView_Previews: PreviewProvider {
static var previews: some View {
MainView()
}
}
As you can see here, the non-selected tab is grey. This is correct:
But the selected tab is blue, instead of black:
Assets:
Asset settings:
If you are supplying your own PNG images for tab views, use rendering mode template so that the default colors are applied to the non-background bits of your images.
If found this easiest to do in the Assets folder. Or you can it in code as:
Image("myImage").renderingMode(.template)
You do not set a selected and not selected icon on your TabView, you only provide one image and it gets rendered in secondary colour when not selected and in accent colour when selected.
If you want your selected tab icons to be black you could set the accent on colour on TabView to black, but it is against Apple's design guidelines and you will change the accent colour of all subviews as well.
Use accentColor of TabView
TabView(selection: $selected) {
SwiftUIMyData()
.tabItem {
Image(systemName: "rectangle.fill.on.rectangle.fill")
Text("My Data")
}
.tag(0)
SwiftUIMyContent()
.tabItem {
Image(systemName: "desktopcomputer")
Text("My Content")
}
.tag(1)
}.accentColor(.black)
Change the rendering mode of your image to original. Either on the xcassets folder, or with the renderingMode modifier in code.
use .accentColor(.black)
import SwiftUI
struct ContentView: View {
#State private var selection = 0
var body: some View {
TabView(selection: $selection){
Text("First View")
.font(.title)
.tabItem {
VStack {
Image("first")
Text("First")
}
}
.tag(0)
Text("Second View")
.font(.title)
.tabItem {
VStack {
Image("second")
Text("Second")
}
}
.tag(1)
}.accentColor(.black)
}
}
tabItem (black) demo