From d936c38c7a8dd10cbb4f2ebad118754fc8ba903a Mon Sep 17 00:00:00 2001 From: Beau Nouvelle Date: Wed, 4 Aug 2021 23:59:11 +1000 Subject: [PATCH] added yyyy-mm-dd format --- DateTextField/DateTextField.swift | 31 ++++++++++++++++++++++++------- Demo/Base.lproj/Main.storyboard | 21 +++++++++------------ Demo/ViewController.swift | 4 ++++ 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/DateTextField/DateTextField.swift b/DateTextField/DateTextField.swift index 91a55b3..89196ea 100644 --- a/DateTextField/DateTextField.swift +++ b/DateTextField/DateTextField.swift @@ -9,7 +9,7 @@ import UIKit import Foundation -protocol DateTextFieldDelegate: class { +protocol DateTextFieldDelegate: AnyObject { func dateDidChange(dateTextField: DateTextField) } @@ -19,6 +19,14 @@ public class DateTextField: UITextField { case monthYear = "MM'$'yyyy" case dayMonthYear = "dd'*'MM'$'yyyy" case monthDayYear = "MM'$'dd'*'yyyy" + case yearMonthDay = "yyyy'&'MM'$'dd" + + func format(separator: String) -> String { + return self.rawValue + .replacingOccurrences(of: "$", with: separator) + .replacingOccurrences(of: "*", with: separator) + .replacingOccurrences(of: "&", with: separator) + } } // MARK: - Properties @@ -35,18 +43,15 @@ public class DateTextField: UITextField { weak var customDelegate: DateTextFieldDelegate? /// Parses the `text` property into a `Date` and returns that date if successful. + /// If unsuccessful, the value will be nil and you'll need to show this in your UI. public var date: Date? { get { - let replacedFirstSymbol = dateFormat.rawValue.replacingOccurrences(of: "$", with: separator) - let format = replacedFirstSymbol.replacingOccurrences(of: "*", with: separator) - dateFormatter.dateFormat = format + dateFormatter.dateFormat = dateFormat.format(separator: separator) return dateFormatter.date(from: text ?? "") } set { if newValue != nil { - let replacedFirstSymbol = dateFormat.rawValue.replacingOccurrences(of: "$", with: separator) - let format = replacedFirstSymbol.replacingOccurrences(of: "*", with: separator) - dateFormatter.dateFormat = format + dateFormatter.dateFormat = dateFormat.format(separator: separator) text = dateFormatter.string(from: newValue!) } else { text = nil @@ -130,6 +135,13 @@ extension DateTextField: UITextFieldDelegate { let month = splitString.count > 0 ? splitString[0] : "" let year = splitString.count > 2 ? splitString[2] : "" textField.text = final(day: day, month: month, year: year) + case .yearMonthDay: + guard numbersOnly.count <= 8 else { return false } + let splitString = split(string: numbersOnly, format: [4, 2, 2]) + let year = splitString.count > 0 ? splitString[0] : "" + let month = splitString.count > 1 ? splitString[1] : "" + let day = splitString.count > 2 ? splitString[2] : "" + textField.text = final(day: day, month: month, year: year) } customDelegate?.dateDidChange(dateTextField: self) return false @@ -174,6 +186,11 @@ extension DateTextField: UITextFieldDelegate { } else { dateString = dateString.replacingOccurrences(of: "$", with: "") } + if year.count >= 4 { + dateString = dateString.replacingOccurrences(of: "&", with: separator) + } else { + dateString = dateString.replacingOccurrences(of: "&", with: "") + } return dateString.replacingOccurrences(of: "'", with: "") } diff --git a/Demo/Base.lproj/Main.storyboard b/Demo/Base.lproj/Main.storyboard index 66fc54a..2f083e9 100644 --- a/Demo/Base.lproj/Main.storyboard +++ b/Demo/Base.lproj/Main.storyboard @@ -1,12 +1,9 @@ - - - - + + - - + @@ -23,30 +20,30 @@ - - + - + + -