Commit 82a26399 by Daniel Dahan

added password TextField example

parent c46898e0
...@@ -38,65 +38,18 @@ under the TextField. ...@@ -38,65 +38,18 @@ under the TextField.
import UIKit import UIKit
import Material import Material
//extension TextField {
//
// func setDefaultLabelSpecs(titleColor titleColor: UIColor, detailColor: UIColor) {
// // ref: https://www.google.com/design/spec/components/text-fields.html#text-fields-labels
// // ref: https://www.google.com/design/spec/patterns/errors.html#errors-user-input-errors
// // ref: https://www.google.com/design/spec/layout/metrics-keylines.html#metrics-keylines-touch-target-size
// assert(height == 80, "Height must be 80, based on Material design spec.")
//
// backgroundColor = nil
//
// let derivedDetailLabelHeight: CGFloat = 15
// let paddingAboveAndBelowErrorText: CGFloat = 4
//
// detailLabel.font = RobotoFont.regularWithSize(12)
// detailLabelActiveColor = detailColor
// detailLabelAnimationDistance = lineLayerThickness + paddingAboveAndBelowErrorText
//
// lineLayerActiveColor = titleColor
// lineLayerColor = MaterialColor.darkText.dividers
// lineLayerDistance = 0 - (derivedDetailLabelHeight + (paddingAboveAndBelowErrorText * 2) + lineLayerThickness)
//
// let derivedTitleLabelHeight: CGFloat = 20
// let paddingAboveLabelText: CGFloat = 8
//
// titleLabelActiveColor = lineLayerActiveColor
// titleLabelAnimationDistance = 0 - (derivedTitleLabelHeight + paddingAboveLabelText)
//
// if let clearButton = clearButton {
// let touchTargetHeight: CGFloat = 48
// let spacing = (height - touchTargetHeight) / 2
// let origin = CGPoint(x: width - touchTargetHeight - spacing, y: spacing)
// let size = CGSize(width: touchTargetHeight, height: touchTargetHeight)
//
// clearButton.frame = CGRect(origin: origin, size: size)
//
// clearButton.contentHorizontalAlignment = .Right
//
// // Use PDF for better rendering
// let clearImage = UIImage(named: "Material/Navigation/Close")
// clearButton.setImage(clearImage, forState: .Normal)
// clearButton.setImage(clearImage, forState: .Highlighted)
//
// // Hide it since it bleeds over the lineLayer, and doesn't look good
// // with the horizontal alignment.
// clearButton.pulseOpacity = 0
// }
// }
//}
class ViewController: UIViewController, TextFieldDelegate { class ViewController: UIViewController, TextFieldDelegate {
private var nameField: MTextField! private var nameField: MTextField!
private var emailField: MTextField! private var emailField: MTextField!
private var passwordField: MTextField!
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
prepareView() prepareView()
prepareResignResponderButton()
prepareNameField() prepareNameField()
prepareEmailField() prepareEmailField()
preparePasswordField()
prepareResignResponderButton()
} }
/// Programmatic update for the textField as it rotates. /// Programmatic update for the textField as it rotates.
...@@ -132,11 +85,11 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -132,11 +85,11 @@ class ViewController: UIViewController, TextFieldDelegate {
/// Prepares the name TextField. /// Prepares the name TextField.
private func prepareNameField() { private func prepareNameField() {
nameField = MTextField() nameField = MTextField()
nameField.placeholder = "Email" nameField.placeholder = "Name"
nameField.detail = "Enter your email address.yyypppggg" nameField.detail = "Your given name"
nameField.clearButtonMode = .WhileEditing nameField.clearButtonMode = .WhileEditing
nameField.textAlignment = .Center nameField.textAlignment = .Center
nameField.text = "daniel@dahan" nameField.text = "Daniel Dahan"
nameField.backgroundColor = MaterialColor.green.accent1 nameField.backgroundColor = MaterialColor.green.accent1
nameField.placeholderLabel.backgroundColor = MaterialColor.green.accent3 nameField.placeholderLabel.backgroundColor = MaterialColor.green.accent3
...@@ -144,8 +97,8 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -144,8 +97,8 @@ class ViewController: UIViewController, TextFieldDelegate {
nameField.translatesAutoresizingMaskIntoConstraints = false nameField.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(nameField) view.addSubview(nameField)
// Size the TextField to the maximum width, less 20 pixels on either side // Size the TextField to the maximum width, less 40 pixels on either side
// with a top margin of 100 pixels. // with a top margin of 40 pixels.
MaterialLayout.alignFromTop(view, child: nameField, top: 40) MaterialLayout.alignFromTop(view, child: nameField, top: 40)
MaterialLayout.alignToParentHorizontally(view, child: nameField, left: 40, right: 40) MaterialLayout.alignToParentHorizontally(view, child: nameField, left: 40, right: 40)
} }
...@@ -169,6 +122,27 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -169,6 +122,27 @@ class ViewController: UIViewController, TextFieldDelegate {
view.addSubview(emailField) view.addSubview(emailField)
} }
/// Prepares the password TextField.
private func preparePasswordField() {
passwordField = MTextField()
passwordField.placeholder = "Password"
passwordField.detail = "At least 8 characters"
passwordField.clearButtonMode = .WhileEditing
passwordField.textAlignment = .Right
passwordField.secureTextEntry = true
passwordField.backgroundColor = MaterialColor.green.accent1
passwordField.placeholderLabel.backgroundColor = MaterialColor.green.accent3
// The translatesAutoresizingMaskIntoConstraints property must be set to enable AutoLayout correctly.
passwordField.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(passwordField)
// Size the TextField to the maximum width, less 40 pixels on either side
// with a top margin of 200 pixels.
MaterialLayout.alignFromTop(view, child: passwordField, top: 200)
MaterialLayout.alignToParentHorizontally(view, child: passwordField, left: 40, right: 40)
}
/// 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 {
return true return true
......
...@@ -252,8 +252,10 @@ public class MTextField : UITextField { ...@@ -252,8 +252,10 @@ public class MTextField : UITextField {
public override func layoutSubviews() { public override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
layoutPlaceholderLabel() if !editing && !animating {
layoutDetailLabel() layoutPlaceholderLabel()
layoutDetailLabel()
}
} }
public override func layoutSublayersOfLayer(layer: CALayer) { public override func layoutSublayersOfLayer(layer: CALayer) {
...@@ -360,25 +362,20 @@ public class MTextField : UITextField { ...@@ -360,25 +362,20 @@ public class MTextField : UITextField {
/// Layout the placeholderLabel. /// Layout the placeholderLabel.
public func layoutPlaceholderLabel() { public func layoutPlaceholderLabel() {
if !editing && !animating { if true == text?.isEmpty {
if true == text?.isEmpty { placeholderLabel.frame = bounds
placeholderLabel.frame = bounds } else if CGAffineTransformIsIdentity(placeholderLabel.transform) {
} else if CGAffineTransformIsIdentity(placeholderLabel.transform) { placeholderLabel.frame = bounds
placeholderLabel.frame = bounds placeholderLabel.transform = CGAffineTransformMakeScale(0.75, 0.75)
placeholderLabel.transform = CGAffineTransformMakeScale(0.75, 0.75) switch textAlignment {
switch textAlignment { case .Left, .Natural:
case .Left, .Natural: placeholderLabel.frame.origin.x = 0
placeholderLabel.frame.origin.x = 0 case .Right:
case .Right: placeholderLabel.frame.origin.x = width - placeholderLabel.frame.width
placeholderLabel.frame.origin.x = width - placeholderLabel.frame.width default:break;
default:break;
}
placeholderLabel.frame.origin.y = -placeholderLabel.frame.size.height
placeholderLabel.textColor = placeholderColor
} }
} else if !animating { placeholderLabel.frame.origin.y = -placeholderLabel.frame.size.height
placeholderLabel.frame.origin.x = 0 placeholderLabel.textColor = placeholderColor
placeholderLabel.frame.size.width = width
} }
} }
......
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