Skip to content

Commit

Permalink
added yyyy-mm-dd format
Browse files Browse the repository at this point in the history
  • Loading branch information
BeauNouvelle committed Aug 4, 2021
1 parent c3f82b0 commit d936c38
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
31 changes: 24 additions & 7 deletions DateTextField/DateTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit
import Foundation

protocol DateTextFieldDelegate: class {
protocol DateTextFieldDelegate: AnyObject {
func dateDidChange(dateTextField: DateTextField)
}

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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: "")
}
Expand Down
21 changes: 9 additions & 12 deletions Demo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="19115.3" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19107.5"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand All @@ -23,30 +20,30 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="center" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="Rmw-HU-cBL" customClass="DateTextField" customModule="DateTextField">
<rect key="frame" x="16" y="70" width="343" height="30"/>
<nil key="textColor"/>
<rect key="frame" x="16" y="50" width="343" height="34"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
</textField>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="fhV-MD-s3s">
<rect key="frame" x="166" y="108" width="42" height="21"/>
<rect key="frame" x="166.5" y="92" width="42" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<segmentedControl opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="Ah7-J2-FbK">
<rect key="frame" x="16" y="150" width="343" height="29"/>
<rect key="frame" x="16" y="134" width="343" height="32"/>
<segments>
<segment title="mm/yyyy"/>
<segment title="dd/mm/yyyy"/>
<segment title="mm/dd/yyyy"/>
<segment title="yyyy/mm/dd"/>
</segments>
<connections>
<action selector="dateFormatChanged:" destination="BYZ-38-t0r" eventType="valueChanged" id="ClS-Fc-Pss"/>
</connections>
</segmentedControl>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="qNw-Y7-p8M">
<rect key="frame" x="113" y="205" width="149" height="30"/>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="qNw-Y7-p8M">
<rect key="frame" x="113" y="192" width="149" height="30"/>
<state key="normal" title="Show Formatted Date"/>
<connections>
<action selector="showDate:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Njk-Pp-6s4"/>
Expand Down
4 changes: 4 additions & 0 deletions Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class ViewController: UIViewController {
dateTextField.placeholder = "MM/DD/YYYY"
dateTextField.text = ""
dateTextField.dateFormat = .monthDayYear
case 3:
dateTextField.placeholder = "YYYY/MM/DD"
dateTextField.text = ""
dateTextField.dateFormat = .yearMonthDay
default:
preconditionFailure("Selected index not handled")
}
Expand Down

0 comments on commit d936c38

Please sign in to comment.