Commit c61e06ce by Daniel Dahan

updated TextField example

parent 3f51240e
...@@ -166,8 +166,6 @@ class AppMenuViewController: MenuViewController { ...@@ -166,8 +166,6 @@ class AppMenuViewController: MenuViewController {
private func showMenuBackdrop() { private func showMenuBackdrop() {
// Disable the side nav, so users can't swipe while viewing the menu. // Disable the side nav, so users can't swipe while viewing the menu.
sideNavigationViewController?.enabled = false sideNavigationViewController?.enabled = false
navigationBarViewController?.navigationBarView.userInteractionEnabled = false
navigationBarViewController?.navigationBarView.alpha = 0.5
menuViewController?.mainViewController.view.userInteractionEnabled = false menuViewController?.mainViewController.view.userInteractionEnabled = false
menuViewController?.mainViewController.view.alpha = 0.5 menuViewController?.mainViewController.view.alpha = 0.5
} }
...@@ -176,8 +174,6 @@ class AppMenuViewController: MenuViewController { ...@@ -176,8 +174,6 @@ class AppMenuViewController: MenuViewController {
private func hideMenuBackdrop() { private func hideMenuBackdrop() {
// Enable the side nav. // Enable the side nav.
sideNavigationViewController?.enabled = true sideNavigationViewController?.enabled = true
navigationBarViewController?.navigationBarView.userInteractionEnabled = true
navigationBarViewController?.navigationBarView.alpha = 1
menuViewController?.mainViewController.view.userInteractionEnabled = true menuViewController?.mainViewController.view.userInteractionEnabled = true
menuViewController?.mainViewController.view.alpha = 1 menuViewController?.mainViewController.view.alpha = 1
} }
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
9626C2F51C7A43CD007CA8E0 /* Material.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9626C2F41C7A43CD007CA8E0 /* Material.framework */; };
9626C2F61C7A43CD007CA8E0 /* Material.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9626C2F41C7A43CD007CA8E0 /* Material.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
966F57A11C226BAA009185B7 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 966F57A01C226BAA009185B7 /* AppDelegate.swift */; }; 966F57A11C226BAA009185B7 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 966F57A01C226BAA009185B7 /* AppDelegate.swift */; };
966F57A31C226BAA009185B7 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 966F57A21C226BAA009185B7 /* ViewController.swift */; }; 966F57A31C226BAA009185B7 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 966F57A21C226BAA009185B7 /* ViewController.swift */; };
966F57A81C226BAA009185B7 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 966F57A71C226BAA009185B7 /* Assets.xcassets */; }; 966F57A81C226BAA009185B7 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 966F57A71C226BAA009185B7 /* Assets.xcassets */; };
...@@ -20,6 +22,7 @@ ...@@ -20,6 +22,7 @@
dstPath = ""; dstPath = "";
dstSubfolderSpec = 10; dstSubfolderSpec = 10;
files = ( files = (
9626C2F61C7A43CD007CA8E0 /* Material.framework in Embed Frameworks */,
); );
name = "Embed Frameworks"; name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
...@@ -27,6 +30,7 @@ ...@@ -27,6 +30,7 @@
/* End PBXCopyFilesBuildPhase section */ /* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
9626C2F41C7A43CD007CA8E0 /* Material.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; name = Material.framework; path = "/Users/danieldahan/Library/Developer/Xcode/DerivedData/Material-hbpnflxhoouqxebjcyhbbhqyesjd/Build/Products/Debug-iphoneos/Material.framework"; sourceTree = "<absolute>"; };
966F579D1C226BAA009185B7 /* TextField.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TextField.app; sourceTree = BUILT_PRODUCTS_DIR; }; 966F579D1C226BAA009185B7 /* TextField.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TextField.app; sourceTree = BUILT_PRODUCTS_DIR; };
966F57A01C226BAA009185B7 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; }; 966F57A01C226BAA009185B7 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
966F57A21C226BAA009185B7 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; }; 966F57A21C226BAA009185B7 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
...@@ -40,6 +44,7 @@ ...@@ -40,6 +44,7 @@
isa = PBXFrameworksBuildPhase; isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
9626C2F51C7A43CD007CA8E0 /* Material.framework in Frameworks */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
...@@ -49,6 +54,7 @@ ...@@ -49,6 +54,7 @@
966F57941C226BAA009185B7 = { 966F57941C226BAA009185B7 = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
9626C2F41C7A43CD007CA8E0 /* Material.framework */,
966F579F1C226BAA009185B7 /* TextField */, 966F579F1C226BAA009185B7 /* TextField */,
966F579E1C226BAA009185B7 /* Products */, 966F579E1C226BAA009185B7 /* Products */,
); );
......
...@@ -39,12 +39,6 @@ import UIKit ...@@ -39,12 +39,6 @@ import UIKit
import Material import Material
class ViewController: UIViewController, TextFieldDelegate { class ViewController: UIViewController, TextFieldDelegate {
/// Name TextField.
private var nameField: TextField!
/// Email TextField.
private var emailField: TextField!
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
prepareView() prepareView()
...@@ -59,49 +53,71 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -59,49 +53,71 @@ class ViewController: UIViewController, TextFieldDelegate {
/// Prepares the name TextField. /// Prepares the name TextField.
private func prepareNameField() { private func prepareNameField() {
nameField = TextField(frame: CGRectMake(57, 100, 300, 24)) let textField: TextField = TextField(frame: CGRectMake(57, 100, 300, 24))
nameField.clearButtonMode = .WhileEditing textField.placeholder = "First Name"
nameField.placeholder = "First Name" textField.placeholderTextColor = MaterialColor.grey.base
nameField.font = RobotoFont.regularWithSize(20) textField.font = RobotoFont.regularWithSize(20)
nameField.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
let image = UIImage(named: "ic_close_white")?.imageWithRenderingMode(.AlwaysTemplate)
let clearButton: FlatButton = FlatButton()
clearButton.pulseColor = MaterialColor.grey.base
clearButton.pulseScale = false
clearButton.tintColor = MaterialColor.grey.base
clearButton.setImage(image, forState: .Normal)
clearButton.setImage(image, forState: .Highlighted)
textField.clearButton = clearButton
nameField.titleLabel = UILabel() view.addSubview(textField)
nameField.titleLabel!.font = RobotoFont.mediumWithSize(12)
nameField.titleLabelColor = MaterialColor.grey.base
nameField.titleLabelActiveColor = MaterialColor.blue.accent3
view.addSubview(nameField)
} }
/// Prepares the email TextField. /// Prepares the email TextField.
private func prepareEmailField() { private func prepareEmailField() {
emailField = TextField(frame: CGRectMake(57, 200, 300, 24)) let textField: TextField = TextField(frame: CGRectMake(57, 200, 300, 24))
emailField.delegate = self textField.delegate = self
emailField.placeholder = "Email" textField.placeholder = "Email"
emailField.font = RobotoFont.regularWithSize(20) textField.placeholderTextColor = MaterialColor.grey.base
emailField.textColor = MaterialColor.black textField.font = RobotoFont.regularWithSize(20)
textField.textColor = MaterialColor.black
emailField.titleLabel = UILabel() textField.titleLabel = UILabel()
emailField.titleLabel!.font = RobotoFont.mediumWithSize(12) textField.titleLabel!.font = RobotoFont.mediumWithSize(12)
emailField.titleLabelColor = MaterialColor.grey.base textField.titleLabelColor = MaterialColor.grey.base
emailField.titleLabelActiveColor = MaterialColor.blue.accent3 textField.titleLabelActiveColor = MaterialColor.blue.accent3
emailField.clearButtonMode = .WhileEditing
/* /*
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() textField.detailLabel = UILabel()
emailField.detailLabel!.text = "Email is incorrect." textField.detailLabel!.text = "Email is incorrect."
emailField.detailLabel!.font = RobotoFont.mediumWithSize(12) textField.detailLabel!.font = RobotoFont.mediumWithSize(12)
emailField.detailLabelActiveColor = MaterialColor.red.accent3 textField.detailLabelActiveColor = MaterialColor.red.accent3
view.addSubview(emailField)
let image = UIImage(named: "ic_close_white")?.imageWithRenderingMode(.AlwaysTemplate)
let clearButton: FlatButton = FlatButton()
clearButton.pulseColor = MaterialColor.grey.base
clearButton.pulseScale = false
clearButton.tintColor = MaterialColor.grey.base
clearButton.setImage(image, forState: .Normal)
clearButton.setImage(image, forState: .Highlighted)
textField.clearButton = clearButton
view.addSubview(textField)
} }
/// 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.resignFirstResponder() (textField as! TextField).detailLabelHidden = 0 == textField.text?.utf16.count
(textField as! TextField).detailLabelHidden = !(0 < emailField.text?.utf16.count)
return false return false
} }
} }
...@@ -41,12 +41,11 @@ public class SearchBarView : StatusBarView { ...@@ -41,12 +41,11 @@ public class SearchBarView : StatusBarView {
/// The UIImage for the clear icon. /// The UIImage for the clear icon.
public var clearButton: UIButton? { public var clearButton: UIButton? {
didSet { get {
if let v: UIButton = clearButton { return textField.clearButton
v.contentEdgeInsets = UIEdgeInsetsZero }
v.addTarget(self, action: "handleClearButton", forControlEvents: .TouchUpInside) set(value) {
} textField.clearButton = value
textField.rightView = clearButton
} }
} }
...@@ -64,22 +63,20 @@ public class SearchBarView : StatusBarView { ...@@ -64,22 +63,20 @@ public class SearchBarView : StatusBarView {
} }
} }
/// Placeholder textColor.
public var placeholderTextColor: UIColor = MaterialColor.black {
didSet {
if let v: String = textField.placeholder {
textField.attributedPlaceholder = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderTextColor])
}
}
}
/// A wrapper for searchBar.placeholder. /// A wrapper for searchBar.placeholder.
public var placeholder: String? { public var placeholder: String? {
didSet { didSet {
textField.placeholder = placeholder textField.placeholder = placeholder
if let v: String = textField.placeholder { }
textField.attributedPlaceholder = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderTextColor]) }
}
/// Placeholder textColor.
public var placeholderTextColor: UIColor {
get {
return textField.placeholderTextColor
}
set(value) {
textField.placeholderTextColor = value
} }
} }
...@@ -114,12 +111,8 @@ public class SearchBarView : StatusBarView { ...@@ -114,12 +111,8 @@ public class SearchBarView : StatusBarView {
public override func reloadView() { public override func reloadView() {
super.reloadView() super.reloadView()
/// Prepare the clearButton.
if let v: UIButton = clearButton {
v.frame = CGRectMake(0, 0, textField.height, textField.height)
}
textField.grid.columns = contentView.grid.columns textField.grid.columns = contentView.grid.columns
textField.reloadView()
grid.reloadLayout() grid.reloadLayout()
} }
...@@ -142,17 +135,11 @@ public class SearchBarView : StatusBarView { ...@@ -142,17 +135,11 @@ public class SearchBarView : StatusBarView {
prepareTextField() prepareTextField()
} }
/// Clears the textField text.
internal func handleClearButton() {
textField.text = ""
}
/// Prepares the textField. /// Prepares the textField.
private func prepareTextField() { private func prepareTextField() {
textField.placeholder = "Search" textField.placeholder = "Search"
textField.backgroundColor = MaterialColor.clear textField.backgroundColor = MaterialColor.clear
textField.clearButtonMode = .Never textField.clearButtonMode = .WhileEditing
textField.rightViewMode = .WhileEditing
contentView.addSubview(textField) contentView.addSubview(textField)
contentView.grid.views = [textField] contentView.grid.views = [textField]
} }
......
...@@ -243,6 +243,23 @@ public class TextField : UITextField { ...@@ -243,6 +243,23 @@ public class TextField : UITextField {
} }
} }
/// The UIImage for the clear icon.
public var clearButton: UIButton? {
didSet {
if let v: UIButton = clearButton {
clearButtonMode = .Never
rightViewMode = .WhileEditing
v.contentEdgeInsets = UIEdgeInsetsZero
v.addTarget(self, action: "handleClearButton", forControlEvents: .TouchUpInside)
} else {
clearButtonMode = .WhileEditing
rightViewMode = .Never
}
rightView = clearButton
reloadView()
}
}
/// The bottom border layer. /// The bottom border layer.
public private(set) lazy var bottomBorderLayer: CAShapeLayer = CAShapeLayer() public private(set) lazy var bottomBorderLayer: CAShapeLayer = CAShapeLayer()
...@@ -285,7 +302,7 @@ public class TextField : UITextField { ...@@ -285,7 +302,7 @@ public class TextField : UITextField {
/// An override to the text property. /// An override to the text property.
public override var text: String? { public override var text: String? {
didSet { didSet {
textFieldDidChange(self) textFieldDidChange()
} }
} }
...@@ -341,6 +358,24 @@ public class TextField : UITextField { ...@@ -341,6 +358,24 @@ public class TextField : UITextField {
} }
} }
/// A wrapper for searchBar.placeholder.
public override var placeholder: String? {
didSet {
if let v: String = placeholder {
attributedPlaceholder = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderTextColor])
}
}
}
/// Placeholder textColor.
public var placeholderTextColor: UIColor = MaterialColor.black {
didSet {
if let v: String = placeholder {
attributedPlaceholder = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderTextColor])
}
}
}
/** /**
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.
...@@ -433,11 +468,37 @@ public class TextField : UITextField { ...@@ -433,11 +468,37 @@ public class TextField : UITextField {
shadowColor = MaterialColor.black shadowColor = MaterialColor.black
borderColor = MaterialColor.black borderColor = MaterialColor.black
masksToBounds = false masksToBounds = false
clearButtonMode = .WhileEditing
prepareBottomBorderLayer() prepareBottomBorderLayer()
} }
/// Reloads the view.
public func reloadView() {
/// Prepare the clearButton.
if let v: UIButton = clearButton {
v.frame = CGRectMake(0, 0, height, height)
}
}
/// Clears the textField text.
internal func handleClearButton() {
text = ""
sendActionsForControlEvents(.ValueChanged)
}
/// Ahdnler when text value changed.
internal func textFieldValueChanged() {
if !detailLabelHidden {
detailLabelHidden = true
MaterialAnimation.animationDisabled { [unowned self] in
self.bottomBorderLayer.backgroundColor = self.titleLabelActiveColor?.CGColor
}
}
}
/// Handler for text editing began. /// Handler for text editing began.
internal func textFieldDidBegin(textField: TextField) { 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
...@@ -445,21 +506,17 @@ public class TextField : UITextField { ...@@ -445,21 +506,17 @@ public class TextField : UITextField {
} }
/// Handler for text changed. /// Handler for text changed.
internal func textFieldDidChange(textField: TextField) { internal func textFieldDidChange() {
if 0 < text?.utf16.count { if 0 < text?.utf16.count {
showTitleLabel() showTitleLabel()
if !detailLabelHidden {
MaterialAnimation.animationDisabled { [unowned self] in
self.bottomBorderLayer.backgroundColor = self.detailLabelActiveColor?.CGColor
}
}
} else if 0 == text?.utf16.count { } else if 0 == text?.utf16.count {
hideTitleLabel() hideTitleLabel()
} }
sendActionsForControlEvents(.ValueChanged)
} }
/// Handler for text editing ended. /// Handler for text editing ended.
internal func textFieldDidEnd(textField: TextField) { internal func textFieldDidEnd() {
if 0 < text?.utf16.count { if 0 < text?.utf16.count {
showTitleLabel() showTitleLabel()
} else if 0 == text?.utf16.count { } else if 0 == text?.utf16.count {
...@@ -488,9 +545,9 @@ public class TextField : UITextField { ...@@ -488,9 +545,9 @@ public class TextField : UITextField {
} else { } else {
v.alpha = 0 v.alpha = 0
} }
addTarget(self, action: "textFieldDidBegin:", forControlEvents: .EditingDidBegin) addTarget(self, action: "textFieldDidBegin", forControlEvents: .EditingDidBegin)
addTarget(self, action: "textFieldDidChange:", forControlEvents: .EditingChanged) addTarget(self, action: "textFieldDidChange", forControlEvents: .EditingChanged)
addTarget(self, action: "textFieldDidEnd:", forControlEvents: .EditingDidEnd) addTarget(self, action: "textFieldDidEnd", forControlEvents: .EditingDidEnd)
} }
} }
...@@ -504,9 +561,12 @@ public class TextField : UITextField { ...@@ -504,9 +561,12 @@ public class TextField : UITextField {
} else { } else {
showDetailLabel() showDetailLabel()
} }
addTarget(self, action: "textFieldDidBegin:", forControlEvents: .EditingDidBegin) if nil == titleLabel {
addTarget(self, action: "textFieldDidChange:", forControlEvents: .EditingChanged) addTarget(self, action: "textFieldDidBegin", forControlEvents: .EditingDidBegin)
addTarget(self, action: "textFieldDidEnd:", forControlEvents: .EditingDidEnd) addTarget(self, action: "textFieldDidChange", forControlEvents: .EditingChanged)
addTarget(self, action: "textFieldDidEnd", forControlEvents: .EditingDidEnd)
}
addTarget(self, action: "textFieldValueChanged", forControlEvents: .ValueChanged)
} }
} }
...@@ -529,7 +589,7 @@ public class TextField : UITextField { ...@@ -529,7 +589,7 @@ public class TextField : UITextField {
v.hidden = false v.hidden = false
UIView.animateWithDuration(0.25, animations: { [unowned self] in UIView.animateWithDuration(0.25, animations: { [unowned self] in
v.alpha = 1 v.alpha = 1
v.frame.origin.y = -v.frame.height - self.titleLabelAnimationDistance v.frame.origin.y -= self.titleLabelAnimationDistance
}) })
} }
} }
...@@ -538,9 +598,9 @@ public class TextField : UITextField { ...@@ -538,9 +598,9 @@ public class TextField : UITextField {
/// Hides and animates the titleLabel property. /// Hides and animates the titleLabel property.
private func hideTitleLabel() { private func hideTitleLabel() {
if let v: UILabel = titleLabel { if let v: UILabel = titleLabel {
UIView.animateWithDuration(0.25, animations: { UIView.animateWithDuration(0.25, animations: { [unowned self] in
v.alpha = 0 v.alpha = 0
v.frame.origin.y = -v.frame.height v.frame.origin.y += self.titleLabelAnimationDistance
}) { _ in }) { _ in
v.hidden = true v.hidden = true
} }
...@@ -565,9 +625,9 @@ public class TextField : UITextField { ...@@ -565,9 +625,9 @@ public class TextField : UITextField {
/// Hides and animates the detailLabel property. /// Hides and animates the detailLabel property.
private func hideDetailLabel() { private func hideDetailLabel() {
if let v: UILabel = detailLabel { if let v: UILabel = detailLabel {
UIView.animateWithDuration(0.25, animations: { UIView.animateWithDuration(0.25, animations: { [unowned self] in
v.alpha = 0 v.alpha = 0
v.frame.origin.y = v.frame.height + 20 v.frame.origin.y -= self.detailLabelAnimationDistance
}) { _ in }) { _ in
v.hidden = true v.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