Merge pull request #1 from iznv/fixes

Fixes
This commit is contained in:
Ivan Zinovyev 2016-12-20 15:45:12 +03:00 committed by GitHub
commit 0aeff35e5c
4 changed files with 56 additions and 77 deletions

View File

@ -1,9 +1,9 @@
Pod::Spec.new do |s|
s.name = 'UIAnimatedTextField'
s.version = '0.1.5'
s.summary = 'UIAnimatedTextField'
s.version = '0.1.6'
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' }

View File

@ -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)
}
}

View File

@ -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
@ -73,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 {
@ -85,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
@ -110,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
@ -170,19 +187,9 @@ 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
@ -407,7 +414,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 {
@ -442,6 +449,8 @@ public class UIAnimatedTextField: UIView {
}
// MARK: - Extension
extension UIAnimatedTextField: UITextFieldDelegate {
public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
@ -461,7 +470,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)
}
}
@ -495,8 +504,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
}

View File

@ -1 +0,0 @@
Example/Pods/Pods.xcodeproj