iOS UIDatePicker "Time" label locale / language not working - ios

Is there any way to change the language/locale of the label with "Time" string on UIDatePicker? This is my date picker configuration:
datePicker.locale = Locale(identifier: "de_DE")
datePicker.calendar.locale = Locale(identifier: "de_DE")
datePicker.datePickerMode = .dateAndTime
datePicker.preferredDatePickerStyle = .inline
And it is displayed as on the screenshot:
As you can see the "Time" label is still in English but I expected it to be in German. Is there any way to fix that or maybe at least hide this label?

Related

Enable next 7day in datepicker in swift

I have implemented date picker in swift. I want to enable the date for next 7 day in calendar.How its is Possible.
You can set a maximum date within a UIDatePicker like this:
var datePicker = UIDatePicker()
datePicker.datePickerMode = .date
// set the start date to today if you don't want to allow dates in the past
datePicker.minimumDate = Date()
// calculate +7 days
let next7Days = Calendar.current.date(byAdding: .day, value: 7, to: Date())
//set maximum date
datePicker.maximumDate = next7Days
this will only allow to chose a date within the next 7 days

UIDatePicker max and min date range

I develop my own app and I work on sign up screen.The person who sign up my app have to enter a birth date in my textfield and only user has 18-55 age accept.My textField has UIDatePicker.When datePicker open ,I want to show min and max date range.For example today now 12/02/2022 , datePicker have to show min date 12/02/2004 (user has 18) and show max date 1/1/1967 (user has 55).
https://imgur.com/a/DBpT5eq
first change the version checking line to
if #available(iOS 13.4,*) {
}
secondly use calendar for min max date
let datePicker = UIDatePicker()
if let eighteenAgo = Calendar.current.date(byAdding: .year, value: -18, to: Date()),
let afterFiftyFive = Calendar.current.date(byAdding: .year, value: 55, to: Date()) {
print(eighteenAgo.formatted(), ": Min")
print(afterFiftyFive.formatted(), ": Max")
datePicker.minimumDate = eighteenAgo
datePicker.maximumDate = afterFiftyFive
}

Bug minimum time in UIDatePicker iOS 14

I am working in UIDatePicker iOS 14 with below config:
datePicker.datePickerMode = .dateAndTime
datePicker.preferredDatePickerStyle = .inline
datePicker.minimumDate = Date() // 29 Dec 2020 at 14:00
When I change the time value and the value smaller than the minimum date (Ex: 29 Dec 2020 at 12:00), the DatePicker updated UI but the value still keeps the minimum date. It's working normally in iOS 13: the date picker returns the minimum value when the change value is less than the minimum value.
Is this the bug of UIDatePicker in iOS?

Refresh UIDatePicker disabled dates color

Description
I have UIDatePicker with datePickerMode set to date, minimumDate and maximumDate are also set. Dates that are after maximumDate are disabled and with different color to indicate that they cannot be selected.
Code for UIDatePicker
let datePicker = UIDatePicker()
datePicker.minimumDate = Date.init(timeIntervalSince1970: 0)
datePicker.maximumDate = Date()
datePicker.setValue(UIColor.yellow, forKeyPath: "textColor")
datePicker.datePickerMode = .date
Disabled future dates
When year is changed to year in the past it's expected that month and date are refreshed and color is changed, which is not the case.
Notice how 2018 has yellow color indicating that it can be selected but August and 26 do not have that color like they are not selectable. But when I scroll to them they will change color to yellow since year is 2017.
Question
How to refresh disabled date picker components when date picker changes value?
The only solution I found is switching datePickerMode to another and switching it back. It works but breaks picker animations :/
#IBAction private func datePickerChangedAction(_ sender: Any) {
self.datePicker.datePickerMode = .dateAndTime
self.datePicker.datePickerMode = .date
// ...
}

Text Label Word Wrapping

I am using a text label to display previously entered user information that has been saved. I am having trouble fitting the whole date and time in one line. Any information that goes beyond a specific length does not appear in the label. I attempted to do two things:
1) make the label larger (this didn't do anything to help with keeping the text within the label)
2) change the "line breaks" option under the attributes inspector to "word wrap". This didn't solve my problem either
How do I make it so that I can include all of the date/time information in the same label, just with wrapped text, so that the text that goes beyond the edge of the screen wraps to the next line?
The code below shows the creation of the view, the screenshots capture what it looks like in the app (some text in "date and time" section not included) and the settings I have set in the storyboard for the label "date and time"
Creating the view:
- (void)configureView
{
Event *theEvent = self.event;
static NSDateFormatter *formatter = nil;
if (formatter == nil) {
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"cccc, MMMM dd, yyyy hh:mm a"];
}
if (theEvent) {
self.detailLabel.text = theEvent.detail;
self.locationLabel.text = theEvent.location;
self.dateTimeLabel.text = [formatter stringFromDate:(NSDate *)theEvent.date];
}
}
The line breaks option only works when you set the number of lines to zero and the label is tall enough to contain more than one line. You can call sizeToThatFits: to get an appropriate size for the label.
You can also use the autoshrink option to automatically make the text smaller when needed.

Resources