From 914e6524ecfbb7eb975fcdffcf291edc55c905e2 Mon Sep 17 00:00:00 2001 From: Ivan Zinovyev Date: Mon, 19 Dec 2016 22:12:22 +0300 Subject: [PATCH 1/6] Replace DateFormatter with Date extension, add dateFormat property for TextField --- ...eFormatter.swift => Date+Extensions.swift} | 39 +++---------------- .../Source/UIAnimatedTextField.swift | 7 +++- 2 files changed, 10 insertions(+), 36 deletions(-) rename UIAnimatedTextField/Source/{TIDateFormatter.swift => Date+Extensions.swift} (50%) diff --git a/UIAnimatedTextField/Source/TIDateFormatter.swift b/UIAnimatedTextField/Source/Date+Extensions.swift similarity index 50% rename from UIAnimatedTextField/Source/TIDateFormatter.swift rename to UIAnimatedTextField/Source/Date+Extensions.swift index daf42cc..299dbb3 100644 --- a/UIAnimatedTextField/Source/TIDateFormatter.swift +++ b/UIAnimatedTextField/Source/Date+Extensions.swift @@ -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) } } diff --git a/UIAnimatedTextField/Source/UIAnimatedTextField.swift b/UIAnimatedTextField/Source/UIAnimatedTextField.swift index 853b6ae..b3c7838 100644 --- a/UIAnimatedTextField/Source/UIAnimatedTextField.swift +++ b/UIAnimatedTextField/Source/UIAnimatedTextField.swift @@ -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) } } From f3d0fe080f6386bbfb3c74a718ab4b15ecce3790 Mon Sep 17 00:00:00 2001 From: Ivan Zinovyev Date: Mon, 19 Dec 2016 22:12:50 +0300 Subject: [PATCH 2/6] Move space to constants --- UIAnimatedTextField/Source/UIAnimatedTextField.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UIAnimatedTextField/Source/UIAnimatedTextField.swift b/UIAnimatedTextField/Source/UIAnimatedTextField.swift index b3c7838..8ade0bf 100644 --- a/UIAnimatedTextField/Source/UIAnimatedTextField.swift +++ b/UIAnimatedTextField/Source/UIAnimatedTextField.swift @@ -498,8 +498,8 @@ extension UIAnimatedTextField: UITextFieldDelegate { result = delegateResult } - if string == " " { - if textField.text?.characters.count ?? 0 == 0 { + if string == Constants.space { + if textField.text?.isEmpty ?? true { result = false } From 341f6c600f7e0d822cb63b0ae12d78c19e68e166 Mon Sep 17 00:00:00 2001 From: Ivan Zinovyev Date: Mon, 19 Dec 2016 22:18:30 +0300 Subject: [PATCH 3/6] Delete _Pods.xcodeproj --- _Pods.xcodeproj | 1 - 1 file changed, 1 deletion(-) delete mode 120000 _Pods.xcodeproj diff --git a/_Pods.xcodeproj b/_Pods.xcodeproj deleted file mode 120000 index 3c5a8e7..0000000 --- a/_Pods.xcodeproj +++ /dev/null @@ -1 +0,0 @@ -Example/Pods/Pods.xcodeproj \ No newline at end of file From 6ede9f5e214318ab1b2b2286568b10f716a84648 Mon Sep 17 00:00:00 2001 From: Ivan Zinovyev Date: Mon, 19 Dec 2016 22:31:54 +0300 Subject: [PATCH 4/6] Edit podspec --- UIAnimatedTextField.podspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UIAnimatedTextField.podspec b/UIAnimatedTextField.podspec index 2f3b552..98f439d 100644 --- a/UIAnimatedTextField.podspec +++ b/UIAnimatedTextField.podspec @@ -1,9 +1,9 @@ Pod::Spec.new do |s| s.name = 'UIAnimatedTextField' s.version = '0.1.5' - s.summary = 'UIAnimatedTextField' + s.summary = 'UITextField with animated placeholder' s.description = <<-DESC - Animated text field + This custom control can be used as a replacement for UITextField. It comes with 5 different text types: simple, password, url, tappable, date. DESC s.homepage = 'https://github.com/iznv/UIAnimatedTextField' s.license = { :type => 'MIT', :file => 'LICENSE' } From 3225462ea8f5ac81d05a3bf2088084c33ca0bd7b Mon Sep 17 00:00:00 2001 From: Ivan Zinovyev Date: Tue, 20 Dec 2016 15:10:30 +0300 Subject: [PATCH 5/6] Add some MARKs --- .../Source/UIAnimatedTextField.swift | 76 ++++++++++--------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/UIAnimatedTextField/Source/UIAnimatedTextField.swift b/UIAnimatedTextField/Source/UIAnimatedTextField.swift index 8ade0bf..61b0497 100644 --- a/UIAnimatedTextField/Source/UIAnimatedTextField.swift +++ b/UIAnimatedTextField/Source/UIAnimatedTextField.swift @@ -75,7 +75,42 @@ public class UIAnimatedTextField: UIView { private var disclosureIndicatorImageView: UIImageView! - // MARK: - Properties + // MARK: - @IBInspectable Properties + + @IBInspectable public var placeholder: String? { + get { + return placeholderLabel.text + } + set { + placeholderLabel.text = newValue + } + } + + @IBInspectable public var isLeftTextAlignment: Bool { + get { + return textField.textAlignment == .left + } + set { + let alignment: NSTextAlignment = newValue ? .left : .center + textField.textAlignment = alignment + placeholderLabel.textAlignment = alignment + } + } + + @IBInspectable public var placeholderTopColor: UIColor = UIColor.gray + @IBInspectable public var placeholderBottomColor: UIColor = UIColor.gray + + @IBInspectable public var enteredTextColor: UIColor { + get { return textField.textColor ?? UIColor.black } + set { textField.textColor = newValue } + } + + @IBInspectable public var lineColor: UIColor { + get { return lineView.backgroundColor ?? UIColor.gray } + set { lineView.backgroundColor = newValue } + } + + // MARK: - Public Properties public var text: String? { get { @@ -87,16 +122,7 @@ public class UIAnimatedTextField: UIView { layoutSubviews() } } - - @IBInspectable public var placeholder: String? { - get { - return placeholderLabel.text - } - set { - placeholderLabel.text = newValue - } - } - + public var font: UIFont? { get { return placeholderLabel.font @@ -112,18 +138,7 @@ public class UIAnimatedTextField: UIView { disclosureIndicatorImageView.isHidden = !isDisclosureIndicatorVisible } } - - @IBInspectable public var isLeftTextAlignment: Bool { - get { - return textField.textAlignment == .left - } - set { - let alignment: NSTextAlignment = newValue ? .left : .center - textField.textAlignment = alignment - placeholderLabel.textAlignment = alignment - } - } - + public var type: TextType = .simple { didSet { textField.isSecureTextEntry = false @@ -174,18 +189,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 - - @IBInspectable public var enteredTextColor: UIColor { - get { return textField.textColor ?? UIColor.black } - set { textField.textColor = newValue } - } - - @IBInspectable public var lineColor: UIColor { - get { return lineView.backgroundColor ?? UIColor.gray } - set { lineView.backgroundColor = newValue } - } + // MARK: - Static Properties static public let doneTitle: String = Constants.done static public let animationDuration: TimeInterval = 0.3 @@ -445,6 +449,8 @@ public class UIAnimatedTextField: UIView { } +// MARK: - Extension + extension UIAnimatedTextField: UITextFieldDelegate { public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { From 467797928e97c342d0631f136e414495550ed559 Mon Sep 17 00:00:00 2001 From: Ivan Zinovyev Date: Tue, 20 Dec 2016 15:44:31 +0300 Subject: [PATCH 6/6] Change version --- UIAnimatedTextField.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UIAnimatedTextField.podspec b/UIAnimatedTextField.podspec index 98f439d..dc79ed2 100644 --- a/UIAnimatedTextField.podspec +++ b/UIAnimatedTextField.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'UIAnimatedTextField' - s.version = '0.1.5' + s.version = '0.1.6' s.summary = 'UITextField with animated placeholder' s.description = <<-DESC This custom control can be used as a replacement for UITextField. It comes with 5 different text types: simple, password, url, tappable, date.