Commit 49c98d26 by Daniel Dahan

updated TextField to handle hiding the titleLabel appropriately

parent c69187bd
...@@ -58,6 +58,7 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -58,6 +58,7 @@ class ViewController: UIViewController, TextFieldDelegate {
private func prepareNameField() { private func prepareNameField() {
nameField = TextField() nameField = TextField()
nameField.placeholder = "Name" nameField.placeholder = "Name"
nameField.delegate = self
view.addSubview(nameField) view.addSubview(nameField)
nameField.translatesAutoresizingMaskIntoConstraints = false nameField.translatesAutoresizingMaskIntoConstraints = false
...@@ -78,9 +79,8 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -78,9 +79,8 @@ class ViewController: UIViewController, TextFieldDelegate {
Used to display the error message, which is displayed when Used to display the error message, which is displayed when
the user presses the 'return' key. the user presses the 'return' key.
*/ */
emailField.detailLabel = UILabel() emailField.detailLabel.text = "Email is incorrect."
emailField.detailLabel!.text = "Email is incorrect." emailField.detailLabel.font = RobotoFont.regularWithSize(12)
emailField.detailLabel!.font = RobotoFont.regularWithSize(12)
emailField.detailLabelActiveColor = MaterialColor.red.accent3 emailField.detailLabelActiveColor = MaterialColor.red.accent3
// emailField.detailLabelAutoHideEnabled = false // Uncomment this line to have manual hiding. // emailField.detailLabelAutoHideEnabled = false // Uncomment this line to have manual hiding.
...@@ -89,7 +89,8 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -89,7 +89,8 @@ class ViewController: UIViewController, TextFieldDelegate {
/// Executed when the 'return' key is pressed when using the emailField. /// Executed when the 'return' key is pressed when using the emailField.
func textFieldShouldReturn(textField: UITextField) -> Bool { func textFieldShouldReturn(textField: UITextField) -> Bool {
(textField as! TextField).detailLabelHidden = 0 == textField.text?.utf16.count emailField.text = nil
// (textField as! TextField).detailLabelHidden = 0 == textField.text?.utf16.count
return true return true
} }
......
...@@ -318,11 +318,7 @@ public class TextField : UITextField { ...@@ -318,11 +318,7 @@ public class TextField : UITextField {
The detail UILabel that is displayed when the detailLabelHidden property The detail UILabel that is displayed when the detailLabelHidden property
is set to false. is set to false.
*/ */
public var detailLabel: UILabel? { @IBInspectable public private(set) var detailLabel: UILabel!
didSet {
prepareDetailLabel()
}
}
/** /**
The color of the detailLabel text when the detailLabelHidden property The color of the detailLabel text when the detailLabelHidden property
...@@ -331,7 +327,7 @@ public class TextField : UITextField { ...@@ -331,7 +327,7 @@ public class TextField : UITextField {
@IBInspectable public var detailLabelActiveColor: UIColor? { @IBInspectable public var detailLabelActiveColor: UIColor? {
didSet { didSet {
if !detailLabelHidden { if !detailLabelHidden {
detailLabel?.textColor = detailLabelActiveColor detailLabel.textColor = detailLabelActiveColor
if nil == lineLayerDetailActiveColor { if nil == lineLayerDetailActiveColor {
lineLayerDetailActiveColor = detailLabelActiveColor lineLayerDetailActiveColor = detailLabelActiveColor
} }
...@@ -355,11 +351,11 @@ public class TextField : UITextField { ...@@ -355,11 +351,11 @@ public class TextField : UITextField {
@IBInspectable public var detailLabelHidden: Bool = true { @IBInspectable public var detailLabelHidden: Bool = true {
didSet { didSet {
if detailLabelHidden { if detailLabelHidden {
detailLabel?.textColor = titleLabelColor detailLabel.textColor = titleLabelColor
lineLayer.backgroundColor = (editing ? lineLayerActiveColor : lineLayerColor)?.CGColor lineLayer.backgroundColor = (editing ? lineLayerActiveColor : lineLayerColor)?.CGColor
hideDetailLabel() hideDetailLabel()
} else { } else {
detailLabel?.textColor = detailLabelActiveColor detailLabel.textColor = detailLabelActiveColor
lineLayer.backgroundColor = (nil == lineLayerDetailActiveColor ? detailLabelActiveColor : lineLayerDetailActiveColor)?.CGColor lineLayer.backgroundColor = (nil == lineLayerDetailActiveColor ? detailLabelActiveColor : lineLayerDetailActiveColor)?.CGColor
showDetailLabel() showDetailLabel()
} }
...@@ -367,14 +363,12 @@ public class TextField : UITextField { ...@@ -367,14 +363,12 @@ public class TextField : UITextField {
} }
/// An override to the text property. /// An override to the text property.
@IBInspectable public override var text: String? { // @IBInspectable public override var text: String? {
didSet { // didSet {
handleValueChanged() // handleValueChanged()
if !editing && (nil == text || text!.isEmpty) { // handleEditingDidEnd()
hideTitleLabel() // }
} // }
}
}
/// Sets the placeholder value. /// Sets the placeholder value.
@IBInspectable public override var placeholder: String? { @IBInspectable public override var placeholder: String? {
...@@ -391,7 +385,7 @@ public class TextField : UITextField { ...@@ -391,7 +385,7 @@ public class TextField : UITextField {
placeholderText = nil placeholderText = nil
} }
} }
if 0 < text?.utf16.count { if false == text?.isEmpty {
titleLabel.text = placeholderText titleLabel.text = placeholderText
} }
} }
...@@ -406,6 +400,13 @@ public class TextField : UITextField { ...@@ -406,6 +400,13 @@ public class TextField : UITextField {
} }
} }
/// An override to the text property.
@IBInspectable public override var text: String? {
didSet {
}
}
/** /**
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.
...@@ -513,6 +514,7 @@ public class TextField : UITextField { ...@@ -513,6 +514,7 @@ public class TextField : UITextField {
prepareClearButton() prepareClearButton()
prepareTitleLabel() prepareTitleLabel()
prepareLineLayer() prepareLineLayer()
prepareDetailLabel()
addTarget(self, action: #selector(handleEditingDidBegin), forControlEvents: .EditingDidBegin) addTarget(self, action: #selector(handleEditingDidBegin), forControlEvents: .EditingDidBegin)
addTarget(self, action: #selector(handleEditingChanged), forControlEvents: .EditingChanged) addTarget(self, action: #selector(handleEditingChanged), forControlEvents: .EditingChanged)
addTarget(self, action: #selector(handleEditingDidEnd), forControlEvents: .EditingDidEnd) addTarget(self, action: #selector(handleEditingDidEnd), forControlEvents: .EditingDidEnd)
...@@ -530,6 +532,7 @@ public class TextField : UITextField { ...@@ -530,6 +532,7 @@ public class TextField : UITextField {
return return
} }
text = nil text = nil
handleValueChanged()
} }
/// Ahdnler when text value changed. /// Ahdnler when text value changed.
...@@ -551,6 +554,9 @@ public class TextField : UITextField { ...@@ -551,6 +554,9 @@ public class TextField : UITextField {
/// Handler for text editing ended. /// Handler for text editing ended.
internal func handleEditingDidEnd() { internal func handleEditingDidEnd() {
if true == text?.isEmpty {
hideTitleLabel()
}
titleLabel.textColor = titleLabelColor titleLabel.textColor = titleLabelColor
lineLayer.frame.size.height = lineLayerThickness lineLayer.frame.size.height = lineLayerThickness
lineLayer.backgroundColor = (detailLabelHidden ? nil == lineLayerColor ? titleLabelColor : lineLayerColor : nil == lineLayerDetailColor ? detailLabelActiveColor : lineLayerDetailColor)?.CGColor lineLayer.backgroundColor = (detailLabelHidden ? nil == lineLayerColor ? titleLabelColor : lineLayerColor : nil == lineLayerDetailColor ? detailLabelActiveColor : lineLayerDetailColor)?.CGColor
...@@ -579,23 +585,22 @@ public class TextField : UITextField { ...@@ -579,23 +585,22 @@ public class TextField : UITextField {
titleLabelColor = placeholderTextColor titleLabelColor = placeholderTextColor
titleLabelActiveColor = MaterialColor.blue.accent3 titleLabelActiveColor = MaterialColor.blue.accent3
if 0 < text?.utf16.count { if false == text?.isEmpty {
showTitleLabel() showTitleLabel()
} }
} }
/// Prepares the detailLabel. /// Prepares the detailLabel.
private func prepareDetailLabel() { private func prepareDetailLabel() {
if let v: UILabel = detailLabel { detailLabel = UILabel(frame: CGRectZero)
v.hidden = true detailLabel.hidden = true
addSubview(v) addSubview(detailLabel)
if detailLabelHidden { if detailLabelHidden {
v.alpha = 0 detailLabel.alpha = 0
} else { } else {
showDetailLabel() showDetailLabel()
} }
} }
}
/// Prepares the lineLayer. /// Prepares the lineLayer.
private func prepareLineLayer() { private func prepareLineLayer() {
...@@ -642,9 +647,9 @@ public class TextField : UITextField { ...@@ -642,9 +647,9 @@ public class TextField : UITextField {
titleLabel.text = placeholderText titleLabel.text = placeholderText
titleLabel.hidden = false titleLabel.hidden = false
UIView.animateWithDuration(0.15, animations: { [weak self] in UIView.animateWithDuration(0.15, animations: { [weak self] in
if let v: TextField = self { if let s: TextField = self {
v.titleLabel.transform = CGAffineTransformScale(v.titleLabel.transform, 0.75, 0.75) s.titleLabel.transform = CGAffineTransformScale(s.titleLabel.transform, 0.75, 0.75)
v.titleLabel.frame = CGRectMake(0, -(v.titleLabelAnimationDistance + h), v.bounds.width, h) s.titleLabel.frame = CGRectMake(0, -(s.titleLabelAnimationDistance + h), s.bounds.width, h)
} }
}) })
} }
...@@ -653,45 +658,43 @@ public class TextField : UITextField { ...@@ -653,45 +658,43 @@ public class TextField : UITextField {
/// Hides and animates the titleLabel property. /// Hides and animates the titleLabel property.
private func hideTitleLabel() { private func hideTitleLabel() {
UIView.animateWithDuration(0.15, animations: { [weak self] in UIView.animateWithDuration(0.15, animations: { [weak self] in
if let v: TextField = self { if let s: TextField = self {
v.titleLabel.transform = CGAffineTransformIdentity s.titleLabel.transform = CGAffineTransformIdentity
v.titleLabel.frame = v.bounds s.titleLabel.frame = s.bounds
} }
}) { [weak self] _ in }) { [weak self] _ in
if let v: TextField = self { if let s: TextField = self {
v.titleLabel.hidden = true s.titleLabel.hidden = true
v.placeholder = v.placeholderText s.placeholder = s.placeholderText
} }
} }
} }
/// Shows and animates the detailLabel property. /// Shows and animates the detailLabel property.
private func showDetailLabel() { private func showDetailLabel() {
if let v: UILabel = detailLabel { if detailLabel.hidden {
if v.hidden { let h: CGFloat = ceil(detailLabel.font.lineHeight)
let h: CGFloat = ceil(v.font.lineHeight) detailLabel.frame = CGRectMake(0, bounds.height + lineLayerDistance, bounds.width, h)
v.frame = CGRectMake(0, bounds.height + lineLayerDistance, bounds.width, h) detailLabel.hidden = false
v.hidden = false
UIView.animateWithDuration(0.15, animations: { [weak self] in UIView.animateWithDuration(0.15, animations: { [weak self] in
if let s: TextField = self { if let s: TextField = self {
v.frame.origin.y = s.frame.height + s.lineLayerDistance + s.detailLabelAnimationDistance s.detailLabel.frame.origin.y = s.frame.height + s.lineLayerDistance + s.detailLabelAnimationDistance
v.alpha = 1 s.detailLabel.alpha = 1
} }
}) })
} }
} }
}
/// Hides and animates the detailLabel property. /// Hides and animates the detailLabel property.
private func hideDetailLabel() { private func hideDetailLabel() {
if let v: UILabel = detailLabel {
UIView.animateWithDuration(0.15, animations: { [weak self] in UIView.animateWithDuration(0.15, animations: { [weak self] in
if let s: TextField = self { if let s: TextField = self {
v.alpha = 0 s.detailLabel.alpha = 0
v.frame.origin.y -= s.detailLabelAnimationDistance s.detailLabel.frame.origin.y -= s.detailLabelAnimationDistance
} }
}) { _ in }) { [weak self] _ in
v.hidden = true if let s: TextField = self {
s.detailLabel.hidden = true
} }
} }
} }
......
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