Commit 1cce513d by Daniel Dahan

updated TextField to prepare titleLabel automatically

parent a78719cd
...@@ -59,11 +59,6 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -59,11 +59,6 @@ class ViewController: UIViewController, TextFieldDelegate {
textField.font = RobotoFont.regularWithSize(20) textField.font = RobotoFont.regularWithSize(20)
textField.textColor = MaterialColor.black textField.textColor = MaterialColor.black
textField.titleLabel = UILabel()
textField.titleLabel!.font = RobotoFont.mediumWithSize(12)
textField.titleLabelColor = MaterialColor.grey.base
textField.titleLabelActiveColor = MaterialColor.blue.accent3
view.addSubview(textField) view.addSubview(textField)
} }
...@@ -76,11 +71,6 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -76,11 +71,6 @@ class ViewController: UIViewController, TextFieldDelegate {
textField.font = RobotoFont.regularWithSize(20) textField.font = RobotoFont.regularWithSize(20)
textField.textColor = MaterialColor.black textField.textColor = MaterialColor.black
textField.titleLabel = UILabel()
textField.titleLabel!.font = RobotoFont.mediumWithSize(12)
textField.titleLabelColor = MaterialColor.grey.base
textField.titleLabelActiveColor = MaterialColor.blue.accent3
/* /*
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.
......
...@@ -26,7 +26,7 @@ Express your creativity with Material, an animation and graphics framework for G ...@@ -26,7 +26,7 @@ Express your creativity with Material, an animation and graphics framework for G
## Requirements ## Requirements
* iOS 8.0+ * iOS 8.0+ / Mac OS X 10.9+
* Xcode 7.3+ * Xcode 7.3+
## Communication ## Communication
...@@ -39,7 +39,7 @@ Express your creativity with Material, an animation and graphics framework for G ...@@ -39,7 +39,7 @@ Express your creativity with Material, an animation and graphics framework for G
## Installation ## Installation
> **Embedded frameworks require a minimum deployment target of iOS 8.** > **Embedded frameworks require a minimum deployment target of iOS 8 or OS X Mavericks (10.9).**
> - [Download Material](https://github.com/CosmicMind/Material/archive/master.zip) > - [Download Material](https://github.com/CosmicMind/Material/archive/master.zip)
Visit the [Installation](https://github.com/CosmicMind/Material/wiki/Installation) page to learn how to install Material using [CocoaPods](http://cocoapods.org) and [Carthage](https://github.com/Carthage/Carthage). Visit the [Installation](https://github.com/CosmicMind/Material/wiki/Installation) page to learn how to install Material using [CocoaPods](http://cocoapods.org) and [Carthage](https://github.com/Carthage/Carthage).
......
...@@ -292,16 +292,12 @@ public class TextField : UITextField { ...@@ -292,16 +292,12 @@ public class TextField : UITextField {
titleLabel text value is updated with the placeholder text titleLabel text value is updated with the placeholder text
value before being displayed. value before being displayed.
*/ */
public var titleLabel: UILabel? { @IBInspectable public private(set) var titleLabel: UILabel!
didSet {
prepareTitleLabel()
}
}
/// The color of the titleLabel text when the textField is not active. /// The color of the titleLabel text when the textField is not active.
@IBInspectable public var titleLabelColor: UIColor? { @IBInspectable public var titleLabelColor: UIColor? {
didSet { didSet {
titleLabel?.textColor = titleLabelColor titleLabel.textColor = titleLabelColor
MaterialAnimation.animationDisabled { [unowned self] in MaterialAnimation.animationDisabled { [unowned self] in
self.bottomBorderLayer.backgroundColor = self.titleLabelColor?.CGColor self.bottomBorderLayer.backgroundColor = self.titleLabelColor?.CGColor
} }
...@@ -501,6 +497,7 @@ public class TextField : UITextField { ...@@ -501,6 +497,7 @@ public class TextField : UITextField {
masksToBounds = false masksToBounds = false
prepareBottomBorderLayer() prepareBottomBorderLayer()
prepareClearButton() prepareClearButton()
prepareTitleLabel()
reloadView() reloadView()
} }
...@@ -530,7 +527,7 @@ public class TextField : UITextField { ...@@ -530,7 +527,7 @@ public class TextField : UITextField {
/// Handler for text editing began. /// Handler for text editing began.
internal func textFieldDidBegin() { internal func textFieldDidBegin() {
titleLabel?.textColor = titleLabelActiveColor titleLabel.textColor = titleLabelActiveColor
MaterialAnimation.animationDisabled { [unowned self] in MaterialAnimation.animationDisabled { [unowned self] in
self.bottomBorderLayer.backgroundColor = self.detailLabelHidden ? self.titleLabelActiveColor?.CGColor : self.detailLabelActiveColor?.CGColor self.bottomBorderLayer.backgroundColor = self.detailLabelHidden ? self.titleLabelActiveColor?.CGColor : self.detailLabelActiveColor?.CGColor
} }
...@@ -553,7 +550,7 @@ public class TextField : UITextField { ...@@ -553,7 +550,7 @@ public class TextField : UITextField {
} else if 0 == text?.utf16.count { } else if 0 == text?.utf16.count {
hideTitleLabel() hideTitleLabel()
} }
titleLabel?.textColor = titleLabelColor titleLabel.textColor = titleLabelColor
MaterialAnimation.animationDisabled { [unowned self] in MaterialAnimation.animationDisabled { [unowned self] in
self.bottomBorderLayer.backgroundColor = self.detailLabelHidden ? self.titleLabelColor?.CGColor : self.detailLabelActiveColor?.CGColor self.bottomBorderLayer.backgroundColor = self.detailLabelHidden ? self.titleLabelColor?.CGColor : self.detailLabelActiveColor?.CGColor
} }
...@@ -584,19 +581,22 @@ public class TextField : UITextField { ...@@ -584,19 +581,22 @@ public class TextField : UITextField {
/// Prepares the titleLabel property. /// Prepares the titleLabel property.
private func prepareTitleLabel() { private func prepareTitleLabel() {
if let v: UILabel = titleLabel { titleLabel = UILabel()
v.hidden = true titleLabel.hidden = true
addSubview(v) titleLabel.font = RobotoFont.mediumWithSize(12)
titleLabelColor = MaterialColor.grey.base
titleLabelActiveColor = MaterialColor.blue.accent3
addSubview(titleLabel)
if 0 < text?.utf16.count { if 0 < text?.utf16.count {
showTitleLabel() showTitleLabel()
} else { } else {
v.alpha = 0 titleLabel.alpha = 0
} }
addTarget(self, action: #selector(textFieldDidBegin), forControlEvents: .EditingDidBegin) addTarget(self, action: #selector(textFieldDidBegin), forControlEvents: .EditingDidBegin)
addTarget(self, action: #selector(textFieldDidChange), forControlEvents: .EditingChanged) addTarget(self, action: #selector(textFieldDidChange), forControlEvents: .EditingChanged)
addTarget(self, action: #selector(textFieldDidEnd), forControlEvents: .EditingDidEnd) addTarget(self, action: #selector(textFieldDidEnd), forControlEvents: .EditingDidEnd)
} }
}
/// Prepares the detailLabel property. /// Prepares the detailLabel property.
private func prepareDetailLabel() { private func prepareDetailLabel() {
...@@ -640,33 +640,29 @@ public class TextField : UITextField { ...@@ -640,33 +640,29 @@ public class TextField : UITextField {
/// Shows and animates the titleLabel property. /// Shows and animates the titleLabel property.
private func showTitleLabel() { private func showTitleLabel() {
if let v: UILabel = titleLabel { if titleLabel.hidden {
if v.hidden {
if let s: String = placeholder { if let s: String = placeholder {
if 0 == v.text?.utf16.count || nil == v.text { if 0 == titleLabel.text?.utf16.count || nil == titleLabel.text {
v.text = s titleLabel.text = s
} }
} }
let h: CGFloat = ceil(v.font.lineHeight) let h: CGFloat = ceil(titleLabel.font.lineHeight)
v.frame = CGRectMake(0, -h, bounds.width, h) titleLabel.frame = CGRectMake(0, -h, bounds.width, h)
v.hidden = false titleLabel.hidden = false
UIView.animateWithDuration(0.25, animations: { [unowned self] in UIView.animateWithDuration(0.25, animations: { [unowned self] in
v.alpha = 1 self.titleLabel.alpha = 1
v.frame.origin.y -= self.titleLabelAnimationDistance self.titleLabel.frame.origin.y -= self.titleLabelAnimationDistance
}) })
} }
} }
}
/// Hides and animates the titleLabel property. /// Hides and animates the titleLabel property.
private func hideTitleLabel() { private func hideTitleLabel() {
if let v: UILabel = titleLabel {
UIView.animateWithDuration(0.25, animations: { [unowned self] in UIView.animateWithDuration(0.25, animations: { [unowned self] in
v.alpha = 0 self.titleLabel.alpha = 0
v.frame.origin.y += self.titleLabelAnimationDistance self.titleLabel.frame.origin.y += self.titleLabelAnimationDistance
}) { _ in }) { _ in
v.hidden = true self.titleLabel.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