Replace DateFormatter with Date extension, add dateFormat property for TextField
This commit is contained in:
parent
4f24e4f9cc
commit
914e6524ec
|
|
@ -22,41 +22,12 @@
|
|||
// THE SOFTWARE.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class TIDateFormatter {
|
||||
extension Date {
|
||||
|
||||
fileprivate static let dateLongFormat = "dd/MM/YYYY"
|
||||
fileprivate static let dateShortFormat = "dd/MM/YY"
|
||||
fileprivate static let monthDayFormat = "MMMM d"
|
||||
|
||||
fileprivate let longDateFormatter = DateFormatter()
|
||||
fileprivate let shortDateFormatter = DateFormatter()
|
||||
fileprivate let monthDayDateFormatter = DateFormatter()
|
||||
|
||||
private static let shared = TIDateFormatter()
|
||||
|
||||
// MARK: Init
|
||||
|
||||
private init() {
|
||||
longDateFormatter.dateFormat = TIDateFormatter.dateLongFormat
|
||||
shortDateFormatter.dateFormat = TIDateFormatter.dateShortFormat
|
||||
monthDayDateFormatter.dateFormat = TIDateFormatter.monthDayFormat
|
||||
}
|
||||
|
||||
// MARK: Public functions
|
||||
|
||||
static func longDate(from date: Date) -> String {
|
||||
return shared.longDateFormatter.string(from: date)
|
||||
}
|
||||
|
||||
static func shortDate(from date: Date) -> String {
|
||||
return shared.shortDateFormatter.string(from: date)
|
||||
}
|
||||
|
||||
/// - Returns: example: "December 2"
|
||||
static func monthDay(from date: Date) -> String {
|
||||
return shared.monthDayDateFormatter.string(from: date)
|
||||
func toString(withFormat format: String) -> String {
|
||||
let dateFormatter = DateFormatter()
|
||||
dateFormatter.dateFormat = format
|
||||
return dateFormatter.string(from: self)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -59,6 +59,8 @@ public class UIAnimatedTextField: UIView {
|
|||
// MARK: - Constants
|
||||
struct Constants {
|
||||
static let done = "Done"
|
||||
static let space = " "
|
||||
static let defaultDateFormat = "dd/MM/YYYY"
|
||||
}
|
||||
|
||||
// MARK: - Delegate
|
||||
|
|
@ -170,6 +172,7 @@ public class UIAnimatedTextField: UIView {
|
|||
}
|
||||
|
||||
public var selectedDate: Date?
|
||||
public var dateFormat: String = Constants.defaultDateFormat
|
||||
|
||||
@IBInspectable public var placeholderTopColor: UIColor = UIColor.gray
|
||||
@IBInspectable public var placeholderBottomColor: UIColor = UIColor.gray
|
||||
|
|
@ -407,7 +410,7 @@ public class UIAnimatedTextField: UIView {
|
|||
|
||||
@objc private func datePickerValueChanged(_ datePicker: UIDatePicker) {
|
||||
selectedDate = datePicker.date
|
||||
text = TIDateFormatter.longDate(from: datePicker.date)
|
||||
text = datePicker.date.toString(withFormat: dateFormat)
|
||||
}
|
||||
|
||||
private func getDateInputAccessoryView() -> UIView {
|
||||
|
|
@ -461,7 +464,7 @@ extension UIAnimatedTextField: UITextFieldDelegate {
|
|||
|
||||
if case .date = type {
|
||||
if let datePicker = textField.inputView as? UIDatePicker {
|
||||
textField.text = TIDateFormatter.longDate(from: datePicker.date)
|
||||
textField.text = datePicker.date.toString(withFormat: dateFormat)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue