Skip to content

Commit

Permalink
- added "pickerAlignment" to set the date picker modal position
Browse files Browse the repository at this point in the history
- improved the "forceScalePicker" logic
- fixed the initial text
  • Loading branch information
rursache committed Sep 25, 2024
1 parent e2db5cc commit 502f6f5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
48 changes: 36 additions & 12 deletions Files/RSDatePicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RSDatePicker
//
// Created by Radu Ursache - RanduSoft
// v1.7.1
// v1.8.0
//

/*
Expand Down Expand Up @@ -104,7 +104,7 @@ open class RSDatePicker: UIView {
self.dateLabel.textColor = self.textColor
}
}
public var initialText: String = "Select date" {
public var initialText: String = "Select {DATE/TIME}" {
didSet {
guard self.currentDate == nil else { return }
self.didUpdateDate()
Expand All @@ -131,6 +131,7 @@ open class RSDatePicker: UIView {
}
}
public var forceScalePicker: Bool = false
public var pickerAlignment: NSTextAlignment = .right

override open var backgroundColor: UIColor? {
didSet {
Expand Down Expand Up @@ -174,18 +175,21 @@ open class RSDatePicker: UIView {
self.updateUI()
}

open func updateUI() { }
open func updateUI() {

}

private func prepareDatePicker() {
self.hideDateLabel()
self.datePicker.preferredDatePickerStyle = .compact
self.datePicker.layer.zPosition = CGFloat(MAXFLOAT)
self.datePicker.datePickerMode = self.pickerMode?.datePickerMode ?? .date
self.datePicker.alpha = 0.03

self.checkDateLimits()
self.updateMargins()

self.dateLabel.text = self.initialText
self.dateLabel.text = self.initialText.replacingOccurrences(of: "{DATE/TIME}", with: self.datePicker.datePickerMode == .time ? "time" : "date").capitalized
}

private func didUpdateDate() {
Expand Down Expand Up @@ -221,19 +225,39 @@ open class RSDatePicker: UIView {
.filter({ "\($0.classForCoder)" == "_UIDatePickerLinkedLabel" }).first
let datePickerTimeLabel = datePickerSubviews
.filter({ "\($0.classForCoder)" == "_UIDatePickerCompactTimeLabel" }).first
let datePickerButton = datePickerTimeLabel?.subviews
.filter({ "\($0.classForCoder)" == "UIButton" }).first

datePickerLinkedLabel?.alpha = 0
datePickerTimeLabel?.alpha = 0.1 // can't be lower or it won't work
// datePickerTimeLabel?.backgroundColor = .red // debug
datePickerTimeLabel?.backgroundColor = .red // debug

if self.forceScalePicker {
let scaleX: Double = 6
let scaleY: Double = 2.3

if let actualPickerContainer = datePickerLinkedLabel?.superview {
actualPickerContainer.transform = CGAffineTransform(scaleX: scaleX, y: scaleY)
if self.forceScalePicker {
datePickerButton?.subviews.filter({ "\($0.classForCoder)" == "UILabel" }).forEach {
($0 as? UILabel)?.font = .systemFont(ofSize: 500)
}
datePickerTimeLabel?.transform = CGAffineTransform(scaleX: scaleX * 2, y: scaleY)
}

let xTransform: Double ; let yTransform: Double

switch self.pickerAlignment {
case .left:
xTransform = 10
yTransform = 8
case .center:
xTransform = 0.285 * log(self.bounds.width - 218 + 60) - 0.175
yTransform = 1
case .right, .justified, .natural:
xTransform = 0
yTransform = 1
default:
xTransform = 0
yTransform = 1
}

if xTransform != 0 {
datePickerLinkedLabel?.superview?.transform = CGAffineTransform(scaleX: xTransform, y: yTransform)
datePickerTimeLabel?.transform = CGAffineTransform(scaleX: xTransform, y: yTransform)
}
}

Expand Down
8 changes: 4 additions & 4 deletions Files/RSDatePicker.xib
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="21701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="23094" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21678"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23084"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand Down Expand Up @@ -44,7 +44,7 @@
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</stackView>
<datePicker alpha="0.30000001192092896" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" datePickerMode="date" style="compact" translatesAutoresizingMaskIntoConstraints="NO" id="Euo-XS-dzT">
<datePicker alpha="0.29999999999999999" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" datePickerMode="date" style="compact" translatesAutoresizingMaskIntoConstraints="NO" id="Euo-XS-dzT">
<rect key="frame" x="0.0" y="0.0" width="279" height="70"/>
<connections>
<action selector="dateChangedAction:" destination="-1" eventType="valueChanged" id="gfS-Os-z48"/>
Expand All @@ -69,7 +69,7 @@
<resources>
<image name="calendar" catalog="system" width="128" height="107"/>
<systemColor name="labelColor">
<color red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ There is also a callback for receiving the new picked date: `didChangeDate`

You can also get the current date whenever you need by using `currentDate`

> [!NOTE]
> While running on iPad, it's recommended to use `forceScalePicker = true` so the picker will scale to the full width.
>
> There is also the `pickerAlignment` property which can change the date picker modal position
## Full example

```swift
Expand All @@ -55,6 +60,7 @@ self.datePicker.calendarIconTint = .label
self.datePicker.calendarIconSizeMultiplier = 0.8
self.datePicker.calendarIconImage = UIImage(systemName: "calendar")!
self.datePicker.forceScalePicker = true
self.datePicker.pickerAlignment = .center
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
Expand Down

0 comments on commit 502f6f5

Please sign in to comment.