Commit c61e06ce by Daniel Dahan

updated TextField example

parent 3f51240e
......@@ -166,8 +166,6 @@ class AppMenuViewController: MenuViewController {
private func showMenuBackdrop() {
// Disable the side nav, so users can't swipe while viewing the menu.
sideNavigationViewController?.enabled = false
navigationBarViewController?.navigationBarView.userInteractionEnabled = false
navigationBarViewController?.navigationBarView.alpha = 0.5
menuViewController?.mainViewController.view.userInteractionEnabled = false
menuViewController?.mainViewController.view.alpha = 0.5
}
......@@ -176,8 +174,6 @@ class AppMenuViewController: MenuViewController {
private func hideMenuBackdrop() {
// Enable the side nav.
sideNavigationViewController?.enabled = true
navigationBarViewController?.navigationBarView.userInteractionEnabled = true
navigationBarViewController?.navigationBarView.alpha = 1
menuViewController?.mainViewController.view.userInteractionEnabled = true
menuViewController?.mainViewController.view.alpha = 1
}
......
......@@ -7,6 +7,8 @@
objects = {
/* 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 */; };
966F57A31C226BAA009185B7 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 966F57A21C226BAA009185B7 /* ViewController.swift */; };
966F57A81C226BAA009185B7 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 966F57A71C226BAA009185B7 /* Assets.xcassets */; };
......@@ -20,6 +22,7 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
9626C2F61C7A43CD007CA8E0 /* Material.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
......@@ -27,6 +30,7 @@
/* End PBXCopyFilesBuildPhase 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; };
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>"; };
......@@ -40,6 +44,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
9626C2F51C7A43CD007CA8E0 /* Material.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......@@ -49,6 +54,7 @@
966F57941C226BAA009185B7 = {
isa = PBXGroup;
children = (
9626C2F41C7A43CD007CA8E0 /* Material.framework */,
966F579F1C226BAA009185B7 /* TextField */,
966F579E1C226BAA009185B7 /* Products */,
);
......
......@@ -39,12 +39,6 @@ import UIKit
import Material
class ViewController: UIViewController, TextFieldDelegate {
/// Name TextField.
private var nameField: TextField!
/// Email TextField.
private var emailField: TextField!
override func viewDidLoad() {
super.viewDidLoad()
prepareView()
......@@ -59,49 +53,71 @@ class ViewController: UIViewController, TextFieldDelegate {
/// Prepares the name TextField.
private func prepareNameField() {
nameField = TextField(frame: CGRectMake(57, 100, 300, 24))
nameField.clearButtonMode = .WhileEditing
nameField.placeholder = "First Name"
nameField.font = RobotoFont.regularWithSize(20)
nameField.textColor = MaterialColor.black
nameField.titleLabel = UILabel()
nameField.titleLabel!.font = RobotoFont.mediumWithSize(12)
nameField.titleLabelColor = MaterialColor.grey.base
nameField.titleLabelActiveColor = MaterialColor.blue.accent3
view.addSubview(nameField)
let textField: TextField = TextField(frame: CGRectMake(57, 100, 300, 24))
textField.placeholder = "First Name"
textField.placeholderTextColor = MaterialColor.grey.base
textField.font = RobotoFont.regularWithSize(20)
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
view.addSubview(textField)
}
/// Prepares the email TextField.
private func prepareEmailField() {
emailField = TextField(frame: CGRectMake(57, 200, 300, 24))
emailField.delegate = self
emailField.placeholder = "Email"
emailField.font = RobotoFont.regularWithSize(20)
emailField.textColor = MaterialColor.black
emailField.titleLabel = UILabel()
emailField.titleLabel!.font = RobotoFont.mediumWithSize(12)
emailField.titleLabelColor = MaterialColor.grey.base
emailField.titleLabelActiveColor = MaterialColor.blue.accent3
emailField.clearButtonMode = .WhileEditing
let textField: TextField = TextField(frame: CGRectMake(57, 200, 300, 24))
textField.delegate = self
textField.placeholder = "Email"
textField.placeholderTextColor = MaterialColor.grey.base
textField.font = RobotoFont.regularWithSize(20)
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
the user presses the 'return' key.
*/
emailField.detailLabel = UILabel()
emailField.detailLabel!.text = "Email is incorrect."
emailField.detailLabel!.font = RobotoFont.mediumWithSize(12)
emailField.detailLabelActiveColor = MaterialColor.red.accent3
view.addSubview(emailField)
textField.detailLabel = UILabel()
textField.detailLabel!.text = "Email is incorrect."
textField.detailLabel!.font = RobotoFont.mediumWithSize(12)
textField.detailLabelActiveColor = MaterialColor.red.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
view.addSubview(textField)
}
/// Executed when the 'return' key is pressed when using the emailField.
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
(textField as! TextField).detailLabelHidden = !(0 < emailField.text?.utf16.count)
(textField as! TextField).detailLabelHidden = 0 == textField.text?.utf16.count
return false
}
}
......@@ -41,12 +41,11 @@ public class SearchBarView : StatusBarView {
/// The UIImage for the clear icon.
public var clearButton: UIButton? {
didSet {
if let v: UIButton = clearButton {
v.contentEdgeInsets = UIEdgeInsetsZero
v.addTarget(self, action: "handleClearButton", forControlEvents: .TouchUpInside)
get {
return textField.clearButton
}
textField.rightView = clearButton
set(value) {
textField.clearButton = value
}
}
......@@ -64,23 +63,21 @@ 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.
public var placeholder: String? {
didSet {
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
}
}
/// A convenience initializer.
......@@ -114,12 +111,8 @@ public class SearchBarView : StatusBarView {
public override func 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.reloadView()
grid.reloadLayout()
}
......@@ -142,17 +135,11 @@ public class SearchBarView : StatusBarView {
prepareTextField()
}
/// Clears the textField text.
internal func handleClearButton() {
textField.text = ""
}
/// Prepares the textField.
private func prepareTextField() {
textField.placeholder = "Search"
textField.backgroundColor = MaterialColor.clear
textField.clearButtonMode = .Never
textField.rightViewMode = .WhileEditing
textField.clearButtonMode = .WhileEditing
contentView.addSubview(textField)
contentView.grid.views = [textField]
}
......
......@@ -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.
public private(set) lazy var bottomBorderLayer: CAShapeLayer = CAShapeLayer()
......@@ -285,7 +302,7 @@ public class TextField : UITextField {
/// An override to the text property.
public override var text: String? {
didSet {
textFieldDidChange(self)
textFieldDidChange()
}
}
......@@ -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.
- Parameter aDecoder: A NSCoder instance.
......@@ -433,11 +468,37 @@ public class TextField : UITextField {
shadowColor = MaterialColor.black
borderColor = MaterialColor.black
masksToBounds = false
clearButtonMode = .WhileEditing
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.
internal func textFieldDidBegin(textField: TextField) {
internal func textFieldDidBegin() {
titleLabel?.textColor = titleLabelActiveColor
MaterialAnimation.animationDisabled { [unowned self] in
self.bottomBorderLayer.backgroundColor = self.detailLabelHidden ? self.titleLabelActiveColor?.CGColor : self.detailLabelActiveColor?.CGColor
......@@ -445,21 +506,17 @@ public class TextField : UITextField {
}
/// Handler for text changed.
internal func textFieldDidChange(textField: TextField) {
internal func textFieldDidChange() {
if 0 < text?.utf16.count {
showTitleLabel()
if !detailLabelHidden {
MaterialAnimation.animationDisabled { [unowned self] in
self.bottomBorderLayer.backgroundColor = self.detailLabelActiveColor?.CGColor
}
}
} else if 0 == text?.utf16.count {
hideTitleLabel()
}
sendActionsForControlEvents(.ValueChanged)
}
/// Handler for text editing ended.
internal func textFieldDidEnd(textField: TextField) {
internal func textFieldDidEnd() {
if 0 < text?.utf16.count {
showTitleLabel()
} else if 0 == text?.utf16.count {
......@@ -488,9 +545,9 @@ public class TextField : UITextField {
} else {
v.alpha = 0
}
addTarget(self, action: "textFieldDidBegin:", forControlEvents: .EditingDidBegin)
addTarget(self, action: "textFieldDidChange:", forControlEvents: .EditingChanged)
addTarget(self, action: "textFieldDidEnd:", forControlEvents: .EditingDidEnd)
addTarget(self, action: "textFieldDidBegin", forControlEvents: .EditingDidBegin)
addTarget(self, action: "textFieldDidChange", forControlEvents: .EditingChanged)
addTarget(self, action: "textFieldDidEnd", forControlEvents: .EditingDidEnd)
}
}
......@@ -504,9 +561,12 @@ public class TextField : UITextField {
} else {
showDetailLabel()
}
addTarget(self, action: "textFieldDidBegin:", forControlEvents: .EditingDidBegin)
addTarget(self, action: "textFieldDidChange:", forControlEvents: .EditingChanged)
addTarget(self, action: "textFieldDidEnd:", forControlEvents: .EditingDidEnd)
if nil == titleLabel {
addTarget(self, action: "textFieldDidBegin", forControlEvents: .EditingDidBegin)
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 {
v.hidden = false
UIView.animateWithDuration(0.25, animations: { [unowned self] in
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 {
/// Hides and animates the titleLabel property.
private func hideTitleLabel() {
if let v: UILabel = titleLabel {
UIView.animateWithDuration(0.25, animations: {
UIView.animateWithDuration(0.25, animations: { [unowned self] in
v.alpha = 0
v.frame.origin.y = -v.frame.height
v.frame.origin.y += self.titleLabelAnimationDistance
}) { _ in
v.hidden = true
}
......@@ -565,9 +625,9 @@ public class TextField : UITextField {
/// Hides and animates the detailLabel property.
private func hideDetailLabel() {
if let v: UILabel = detailLabel {
UIView.animateWithDuration(0.25, animations: {
UIView.animateWithDuration(0.25, animations: { [unowned self] in
v.alpha = 0
v.frame.origin.y = v.frame.height + 20
v.frame.origin.y -= self.detailLabelAnimationDistance
}) { _ in
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