From 0f5853a8d292acffdf590761bdd544e7636449a7 Mon Sep 17 00:00:00 2001 From: Radu Ursache Date: Wed, 31 May 2023 18:28:17 +0300 Subject: [PATCH] - removed initialDate - small fixes - updated README file --- Files/RSDatePicker.swift | 42 +++++++++++++++++------------------ README.md | 48 ++++++++++++++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 31 deletions(-) diff --git a/Files/RSDatePicker.swift b/Files/RSDatePicker.swift index dff509f..41d5c83 100644 --- a/Files/RSDatePicker.swift +++ b/Files/RSDatePicker.swift @@ -52,7 +52,13 @@ open class RSDatePicker: UIView { } // config - public var initialDate: Date? + public var currentDate: Date? { + didSet { + DispatchQueue.main.async { + self.didUpdateDate() + } + } + } public var minimumDate: Date? { didSet { self.checkDateLimits() @@ -95,7 +101,7 @@ open class RSDatePicker: UIView { self.dateLabel.textColor = self.textColor } } - public var initialText: String? { + public var initialText: String = "Select date" { didSet { guard self.currentDate == nil else { return } self.didUpdateDate() @@ -122,13 +128,7 @@ open class RSDatePicker: UIView { } } public var forceScalePicker: Bool = false - public var currentDate: Date? { - didSet { - DispatchQueue.main.async { - self.didUpdateDate() - } - } - } + override open var backgroundColor: UIColor? { didSet { guard self.view != nil else { return } @@ -179,9 +179,10 @@ open class RSDatePicker: UIView { self.datePicker.layer.zPosition = CGFloat(MAXFLOAT) self.datePicker.datePickerMode = self.pickerMode?.datePickerMode ?? .date self.datePicker.alpha = 0.03 - self.datePicker.date = self.initialDate ?? Date() self.checkDateLimits() self.updateMargins() + + self.dateLabel.text = self.initialText } private func didUpdateDate() { @@ -189,27 +190,24 @@ open class RSDatePicker: UIView { dateFormatter.dateFormat = self.dateFormat ?? (self.pickerMode?.defaultFormat ?? PickerMode.date.defaultFormat) guard let currentDate = self.currentDate else { - if let initialText = self.initialText { - self.dateLabel.text = initialText - } else if let initialDate = self.initialDate { - self.dateLabel.text = dateFormatter.string(from: initialDate) - } else { - self.dateLabel.text = dateFormatter.string(from: Date()) - } + self.dateLabel.text = self.initialText return } - + + self.datePicker.date = currentDate self.dateLabel.text = dateFormatter.string(from: currentDate) } private func checkDateLimits() { self.datePicker.minimumDate = self.minimumDate self.datePicker.maximumDate = self.maximumDate + + guard let currentDate = self.currentDate else { return } - if let minimumDate = self.minimumDate, self.datePicker.date < minimumDate { - self.datePicker.date = minimumDate - } else if let maximumDate = self.maximumDate, self.datePicker.date > maximumDate { - self.datePicker.date = maximumDate + if let minimumDate = self.minimumDate, currentDate < minimumDate { + self.currentDate = minimumDate + } else if let maximumDate = self.maximumDate, currentDate > maximumDate { + self.currentDate = maximumDate } } diff --git a/README.md b/README.md index 57ffb6a..d4e9219 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # RSDatePicker -A beautiful UIView wrapper for UIDatePicker. Requires iOS 13.2 or newer +A beautiful UIView wrapper for UIDatePicker. Requires iOS 13.4 or newer ## Setup - Storyboard @@ -15,21 +15,51 @@ Create a `UIView` and set its custom class to `RSDatePicker` and link it with a You can customize the date picker in multiple ways: -- `initialDate` sets the default date -- `minimumDate` sets the minimum selectable date -- `maximumDate` sets the maximum selectable date -- `pickerMode` sets the date picker mode (`.date` or `time`) -- `dateFormat` sets the date format for the visible label +- `currentDate` sets the default date (optional) +- `initialText` sets the default text if a date is not selected (optional) +- `minimumDate` sets the minimum selectable date (optional) +- `maximumDate` sets the maximum selectable date (optional) +- `pickerMode` sets the date picker mode (`.date` or `time`) (optional) +- `dateFormat` sets the date format for the visible label (optional) - `closeWhenSelectingDate` allows you to enable/disable closing after a date was picked - `closeAnimationDuration` controls the animation speed when closing the picker - `calendarIconIsHidden` controls if the calendar icon is hidden - `calendarIconSizeMultiplier` controls the multiplier of the aspect ratio of the calendar imageview - -You can change the margins (`left/right/top/bottomMargin`) of the container view +- `left/right/top/bottomMargin` changes the margins of the container view There is also a callback for receiving the new picked date: `didChangeDate` -You can also get or set the current date whenever you need by using `currentDate` +You can also get the current date whenever you need by using `currentDate` + +## Full example + +```swift +import RSDatePicker + +(...) + +@IBOutlet weak var datePicker: RSDatePicker! + +(...) + +self.datePicker.cornerRadius = 16 +self.datePicker.leftMargin = 20 +self.datePicker.rightMargin = 16 + +self.datePicker.pickerMode = .date +self.datePicker.currentDate = Calendar.current.date(byAdding: .day, value: -1, to: Date()) // yesterday +self.datePicker.initialText = "Select Date" +self.datePicker.calendarIconIsHidden = false +self.datePicker.calendarIconTint = .label +self.datePicker.calendarIconSizeMultiplier = 0.8 +self.datePicker.forceScalePicker = true +self.datePicker.closeWhenSelectingDate = true +self.datePicker.minimumDate = Calendar.current.date(byAdding: .day, value: -4, to: Date()) // 4 days ago +self.datePicker.maximumDate = Calendar.current.date(byAdding: .day, value: 20, to: Date()) // 20 days in the future +self.datePicker.didChangeDate = { newDate in + print(newDate) +} +``` ## License RSDatePicker is available under the **MPL-2.0 license**. More info available [here](https://www.mozilla.org/en-US/MPL/2.0/).