Commit cb15ef05 by Daniel Dahan

development: issue-594: added new feature where TextField leftView upadtes…

development: issue-594: added new feature where TextField leftView upadtes colors with placeholder and divider
parent 62831272
...@@ -78,4 +78,9 @@ public struct Application { ...@@ -78,4 +78,9 @@ public struct Application {
public static var shouldStatusBarBeHidden: Bool { public static var shouldStatusBarBeHidden: Bool {
return isLandscape && .phone == Device.userInterfaceIdiom return isLandscape && .phone == Device.userInterfaceIdiom
} }
/// A reference to the user interface layout direction.
public static var userInterfaceLayoutDirection: UIUserInterfaceLayoutDirection {
return UIApplication.shared.userInterfaceLayoutDirection
}
} }
...@@ -30,8 +30,6 @@ ...@@ -30,8 +30,6 @@
import UIKit import UIKit
private var TextFieldContext: UInt8 = 0
@objc(TextFieldDelegate) @objc(TextFieldDelegate)
public protocol TextFieldDelegate: UITextFieldDelegate { public protocol TextFieldDelegate: UITextFieldDelegate {
/** /**
...@@ -77,6 +75,11 @@ open class TextField: UITextField { ...@@ -77,6 +75,11 @@ open class TextField: UITextField {
/// A Boolean that indicates if the TextField is in an animating state. /// A Boolean that indicates if the TextField is in an animating state.
open internal(set) var isAnimating = false open internal(set) var isAnimating = false
/// A boolean indicating whether the text is empty.
open var isEmpty: Bool {
return true == text?.isEmpty
}
open override var leftView: UIView? { open override var leftView: UIView? {
didSet { didSet {
prepareLeftView() prepareLeftView()
...@@ -84,11 +87,6 @@ open class TextField: UITextField { ...@@ -84,11 +87,6 @@ open class TextField: UITextField {
} }
} }
/// A boolean indicating whether the text is empty.
open var isEmpty: Bool {
return true == text?.isEmpty
}
/// The leftView width value. /// The leftView width value.
open var leftViewWidth: CGFloat { open var leftViewWidth: CGFloat {
guard nil != leftView else { guard nil != leftView else {
...@@ -101,6 +99,22 @@ open class TextField: UITextField { ...@@ -101,6 +99,22 @@ open class TextField: UITextField {
/// The leftView width value. /// The leftView width value.
open var leftViewOffset: CGFloat = 16 open var leftViewOffset: CGFloat = 16
/// Placeholder normal text
@IBInspectable
open var leftViewNormalColor = Color.darkText.others {
didSet {
updateLeftViewColor()
}
}
/// Placeholder active text
@IBInspectable
open var leftViewActiveColor = Color.blue.base {
didSet {
updateLeftViewColor()
}
}
/// Divider normal height. /// Divider normal height.
@IBInspectable @IBInspectable
open var dividerNormalHeight: CGFloat = 1 { open var dividerNormalHeight: CGFloat = 1 {
...@@ -185,7 +199,6 @@ open class TextField: UITextField { ...@@ -185,7 +199,6 @@ open class TextField: UITextField {
@IBInspectable @IBInspectable
open var placeholderActiveColor = Color.blue.base { open var placeholderActiveColor = Color.blue.base {
didSet { didSet {
tintColor = placeholderActiveColor
updatePlaceholderLabelColor() updatePlaceholderLabelColor()
} }
} }
...@@ -330,26 +343,7 @@ open class TextField: UITextField { ...@@ -330,26 +343,7 @@ open class TextField: UITextField {
} }
} }
open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { /**
guard "placeholderLabel.text" != keyPath else {
updatePlaceholderLabelColor()
return
}
guard "detailLabel.text" != keyPath else {
updateDetailLabelColor()
return
}
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
deinit {
removeObserver(self, forKeyPath: "placeholderLabel.text")
removeObserver(self, forKeyPath: "detailLabel.text")
}
/**
An initializer that initializes the object with a NSCoder object. An initializer that initializes the object with a NSCoder object.
- Parameter aDecoder: A NSCoder instance. - Parameter aDecoder: A NSCoder instance.
*/ */
...@@ -430,7 +424,6 @@ extension TextField { ...@@ -430,7 +424,6 @@ extension TextField {
font = RobotoFont.regular(with: 16) font = RobotoFont.regular(with: 16)
placeholderNormalColor = Color.darkText.others placeholderNormalColor = Color.darkText.others
addSubview(placeholderLabel) addSubview(placeholderLabel)
addObserver(self, forKeyPath: "placeholderLabel.text", options: [], context: &TextFieldContext)
} }
/// Prepares the detailLabel. /// Prepares the detailLabel.
...@@ -439,12 +432,12 @@ extension TextField { ...@@ -439,12 +432,12 @@ extension TextField {
detailLabel.numberOfLines = 0 detailLabel.numberOfLines = 0
detailColor = Color.darkText.others detailColor = Color.darkText.others
addSubview(detailLabel) addSubview(detailLabel)
addObserver(self, forKeyPath: "detailLabel.text", options: [], context: &TextFieldContext)
} }
/// Prepares the leftView. /// Prepares the leftView.
fileprivate func prepareLeftView() { fileprivate func prepareLeftView() {
leftView?.contentMode = .left leftView?.contentMode = .left
updateLeftViewColor()
} }
/// Prepares the target handlers. /// Prepares the target handlers.
...@@ -456,15 +449,23 @@ extension TextField { ...@@ -456,15 +449,23 @@ extension TextField {
/// Prepares the textAlignment. /// Prepares the textAlignment.
fileprivate func prepareTextAlignment() { fileprivate func prepareTextAlignment() {
textAlignment = .rightToLeft == UIApplication.shared.userInterfaceLayoutDirection ? .right : .left textAlignment = .rightToLeft == Application.userInterfaceLayoutDirection ? .right : .left
}
}
extension TextField {
/// Updates the leftView tint color.
fileprivate func updateLeftViewColor() {
leftView?.tintColor = isEditing ? leftViewActiveColor : leftViewNormalColor
} }
/// Updates the placeholderLabel attributedText. /// Updates the placeholderLabel text color.
fileprivate func updatePlaceholderLabelColor() { fileprivate func updatePlaceholderLabelColor() {
tintColor = placeholderActiveColor
placeholderLabel.textColor = isEditing ? placeholderActiveColor : placeholderNormalColor placeholderLabel.textColor = isEditing ? placeholderActiveColor : placeholderNormalColor
} }
/// Updates the detailLabel attributedText. /// Updates the detailLabel text color.
fileprivate func updateDetailLabelColor() { fileprivate func updateDetailLabelColor() {
detailLabel.textColor = detailColor detailLabel.textColor = detailColor
} }
...@@ -535,6 +536,7 @@ extension TextField { ...@@ -535,6 +536,7 @@ extension TextField {
/// Handles the text editing did begin state. /// Handles the text editing did begin state.
@objc @objc
fileprivate func handleEditingDidBegin() { fileprivate func handleEditingDidBegin() {
leftViewEditingBeginAnimation()
placeholderEditingDidBeginAnimation() placeholderEditingDidBeginAnimation()
dividerEditingDidBeginAnimation() dividerEditingDidBeginAnimation()
...@@ -549,6 +551,7 @@ extension TextField { ...@@ -549,6 +551,7 @@ extension TextField {
/// Handles the text editing did end state. /// Handles the text editing did end state.
@objc @objc
fileprivate func handleEditingDidEnd() { fileprivate func handleEditingDidEnd() {
leftViewEditingEndAnimation()
placeholderEditingDidEndAnimation() placeholderEditingDidEndAnimation()
dividerEditingDidEndAnimation() dividerEditingDidEndAnimation()
} }
...@@ -584,6 +587,16 @@ extension TextField { ...@@ -584,6 +587,16 @@ extension TextField {
} }
extension TextField { extension TextField {
/// The animation for leftView when editing begins.
fileprivate func leftViewEditingBeginAnimation() {
updateLeftViewColor()
}
/// The animation for leftView when editing ends.
fileprivate func leftViewEditingEndAnimation() {
updateLeftViewColor()
}
/// The animation for the divider when editing begins. /// The animation for the divider when editing begins.
fileprivate func dividerEditingDidBeginAnimation() { fileprivate func dividerEditingDidBeginAnimation() {
dividerThickness = dividerActiveHeight dividerThickness = dividerActiveHeight
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment