Commit d3650f18 by Daniel Dahan

added color matching when updating tintColor

parent cb996727
...@@ -39,9 +39,9 @@ import UIKit ...@@ -39,9 +39,9 @@ import UIKit
import Material import Material
class ViewController: UIViewController, TextFieldDelegate { class ViewController: UIViewController, TextFieldDelegate {
private var nameField: MTextField! private var nameField: TextField!
private var emailField: MTextField! private var emailField: TextField!
private var passwordField: MTextField! private var passwordField: TextField!
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
...@@ -85,7 +85,7 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -85,7 +85,7 @@ class ViewController: UIViewController, TextFieldDelegate {
/// Prepares the name TextField. /// Prepares the name TextField.
private func prepareNameField() { private func prepareNameField() {
nameField = MTextField() nameField = TextField()
nameField.text = "Daniel Dahan" nameField.text = "Daniel Dahan"
nameField.placeholder = "Name" nameField.placeholder = "Name"
nameField.detail = "Your given name" nameField.detail = "Your given name"
...@@ -106,7 +106,7 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -106,7 +106,7 @@ class ViewController: UIViewController, TextFieldDelegate {
/// Prepares the email TextField. /// Prepares the email TextField.
private func prepareEmailField() { private func prepareEmailField() {
emailField = MTextField(frame: CGRectMake(40, 120, view.bounds.width - 80, 32)) emailField = TextField(frame: CGRectMake(40, 120, view.bounds.width - 80, 32))
emailField.placeholder = "Email" emailField.placeholder = "Email"
emailField.detail = "Error, incorrect email" emailField.detail = "Error, incorrect email"
emailField.enableClearFlatButton = true emailField.enableClearFlatButton = true
...@@ -122,13 +122,16 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -122,13 +122,16 @@ class ViewController: UIViewController, TextFieldDelegate {
/// Prepares the password TextField. /// Prepares the password TextField.
private func preparePasswordField() { private func preparePasswordField() {
passwordField = MTextField() passwordField = TextField()
passwordField.placeholder = "Password" passwordField.placeholder = "Password"
passwordField.detail = "At least 8 characters" passwordField.detail = "At least 8 characters"
passwordField.clearButtonMode = .WhileEditing passwordField.clearButtonMode = .WhileEditing
passwordField.enableVisibilityFlatButton = true passwordField.enableVisibilityFlatButton = true
passwordField.delegate = self passwordField.delegate = self
// Setting the visibilityFlatButton color.
passwordField.visibilityFlatButton?.tintColor = MaterialColor.green.base.colorWithAlphaComponent(passwordField.secureTextEntry ? 0.38 : 0.54)
// The translatesAutoresizingMaskIntoConstraints property must be set to enable AutoLayout correctly. // The translatesAutoresizingMaskIntoConstraints property must be set to enable AutoLayout correctly.
passwordField.translatesAutoresizingMaskIntoConstraints = false passwordField.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(passwordField) view.addSubview(passwordField)
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
96334EF61C8B84660083986B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 96334EF51C8B84660083986B /* Assets.xcassets */; }; 96334EF61C8B84660083986B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 96334EF51C8B84660083986B /* Assets.xcassets */; };
963832421B88DFD80015F710 /* Material.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 963832361B88DFD80015F710 /* Material.framework */; }; 963832421B88DFD80015F710 /* Material.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 963832361B88DFD80015F710 /* Material.framework */; };
9658F1E51CD0229D00B902C1 /* MTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9658F1E41CD0229C00B902C1 /* MTextField.swift */; };
9660161D1CB2ED6C00AAB661 /* Material.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 966016131CB2ED6C00AAB661 /* Material.framework */; }; 9660161D1CB2ED6C00AAB661 /* Material.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 966016131CB2ED6C00AAB661 /* Material.framework */; };
9660162A1CB2F04E00AAB661 /* Material.h in Headers */ = {isa = PBXBuildFile; fileRef = 96D88C091C1328D800B91418 /* Material.h */; settings = {ATTRIBUTES = (Public, ); }; }; 9660162A1CB2F04E00AAB661 /* Material.h in Headers */ = {isa = PBXBuildFile; fileRef = 96D88C091C1328D800B91418 /* Material.h */; settings = {ATTRIBUTES = (Public, ); }; };
9679CEAB1CC69E080021685B /* Material+UIImage+TintColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9679CEAA1CC69E080021685B /* Material+UIImage+TintColor.swift */; }; 9679CEAB1CC69E080021685B /* Material+UIImage+TintColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9679CEAA1CC69E080021685B /* Material+UIImage+TintColor.swift */; };
...@@ -197,7 +196,6 @@ ...@@ -197,7 +196,6 @@
963832361B88DFD80015F710 /* Material.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Material.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 963832361B88DFD80015F710 /* Material.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Material.framework; sourceTree = BUILT_PRODUCTS_DIR; };
963832411B88DFD80015F710 /* Material.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Material.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 963832411B88DFD80015F710 /* Material.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Material.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
963832591B88E31A0015F710 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; }; 963832591B88E31A0015F710 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9658F1E41CD0229C00B902C1 /* MTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MTextField.swift; sourceTree = "<group>"; };
966016131CB2ED6C00AAB661 /* Material.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Material.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 966016131CB2ED6C00AAB661 /* Material.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Material.framework; sourceTree = BUILT_PRODUCTS_DIR; };
9660161C1CB2ED6C00AAB661 /* Material OSX Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Material OSX Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 9660161C1CB2ED6C00AAB661 /* Material OSX Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Material OSX Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
967887881C9777CB0037F6C9 /* MaterialViewTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MaterialViewTests.swift; sourceTree = "<group>"; }; 967887881C9777CB0037F6C9 /* MaterialViewTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MaterialViewTests.swift; sourceTree = "<group>"; };
...@@ -582,7 +580,6 @@ ...@@ -582,7 +580,6 @@
96BCB79C1CB40DC500C806FE /* TextField.swift */, 96BCB79C1CB40DC500C806FE /* TextField.swift */,
96BCB79D1CB40DC500C806FE /* TextStorage.swift */, 96BCB79D1CB40DC500C806FE /* TextStorage.swift */,
96BCB79E1CB40DC500C806FE /* TextView.swift */, 96BCB79E1CB40DC500C806FE /* TextView.swift */,
9658F1E41CD0229C00B902C1 /* MTextField.swift */,
); );
name = Text; name = Text;
sourceTree = "<group>"; sourceTree = "<group>";
...@@ -885,7 +882,6 @@ ...@@ -885,7 +882,6 @@
96BCB7D11CB40DC500C806FE /* MaterialSwitch.swift in Sources */, 96BCB7D11CB40DC500C806FE /* MaterialSwitch.swift in Sources */,
96BCB7BA1CB40DC500C806FE /* MaterialCollectionView.swift in Sources */, 96BCB7BA1CB40DC500C806FE /* MaterialCollectionView.swift in Sources */,
96BCB7A31CB40DC500C806FE /* CapturePreview.swift in Sources */, 96BCB7A31CB40DC500C806FE /* CapturePreview.swift in Sources */,
9658F1E51CD0229D00B902C1 /* MTextField.swift in Sources */,
96BCB7BC1CB40DC500C806FE /* MaterialCollectionViewDataSource.swift in Sources */, 96BCB7BC1CB40DC500C806FE /* MaterialCollectionViewDataSource.swift in Sources */,
96BCB7C71CB40DC500C806FE /* MaterialKeyframeAnimation.swift in Sources */, 96BCB7C71CB40DC500C806FE /* MaterialKeyframeAnimation.swift in Sources */,
96BCB7BE1CB40DC500C806FE /* MaterialCollectionViewLayout.swift in Sources */, 96BCB7BE1CB40DC500C806FE /* MaterialCollectionViewLayout.swift in Sources */,
......
/*
* Copyright (C) 2015 - 2016, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.io>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Material nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import UIKit
public protocol MTextFieldDelegate : UITextFieldDelegate {}
@IBDesignable
public class MTextField : UITextField {
public private(set) var animating: Bool = false
/**
This property is the same as clipsToBounds. It crops any of the view's
contents from bleeding past the view's frame.
*/
@IBInspectable public var masksToBounds: Bool {
get {
return layer.masksToBounds
}
set(value) {
layer.masksToBounds = value
}
}
/// A property that accesses the backing layer's backgroundColor.
@IBInspectable public override var backgroundColor: UIColor? {
didSet {
layer.backgroundColor = backgroundColor?.CGColor
}
}
/// A property that accesses the layer.frame.origin.x property.
@IBInspectable public var x: CGFloat {
get {
return layer.frame.origin.x
}
set(value) {
layer.frame.origin.x = value
}
}
/// A property that accesses the layer.frame.origin.y property.
@IBInspectable public var y: CGFloat {
get {
return layer.frame.origin.y
}
set(value) {
layer.frame.origin.y = value
}
}
/// A property that accesses the layer.frame.size.width property.
@IBInspectable public var width: CGFloat {
get {
return layer.frame.size.width
}
set(value) {
layer.frame.size.width = value
}
}
/// A property that accesses the layer.frame.size.height property.
@IBInspectable public var height: CGFloat {
get {
return layer.frame.size.height
}
set(value) {
layer.frame.size.height = value
}
}
/// A property that accesses the layer.position property.
@IBInspectable public var position: CGPoint {
get {
return layer.position
}
set(value) {
layer.position = value
}
}
/// A property that accesses the layer.zPosition property.
@IBInspectable public var zPosition: CGFloat {
get {
return layer.zPosition
}
set(value) {
layer.zPosition = value
}
}
/// Reference to the divider.
public private(set) lazy var divider: CAShapeLayer = CAShapeLayer()
/// Divider height.
@IBInspectable public var dividerHeight: CGFloat = 1
/// Divider active state height.
@IBInspectable public var dividerActiveHeight: CGFloat = 2
/// Sets the divider.
@IBInspectable public var dividerColor: UIColor = MaterialColor.darkText.dividers {
didSet {
if !editing {
divider.backgroundColor = dividerColor.CGColor
}
}
}
/// Sets the divider.
@IBInspectable public var dividerActiveColor: UIColor? {
didSet {
if let v: UIColor = dividerActiveColor {
if editing {
divider.backgroundColor = v.CGColor
}
}
}
}
/// The placeholderLabel font value.
@IBInspectable public override var font: UIFont? {
get {
return placeholderLabel.font
}
set(value) {
placeholderLabel.font = value
}
}
/// The placeholderLabel text value.
@IBInspectable public override var placeholder: String? {
get {
return placeholderLabel.text
}
set(value) {
placeholderLabel.text = value
if let v: String = value {
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderColor])
}
}
}
/**
The placeholderLabel UILabel that is displayed when there is text. The
placeholderLabel text value is updated with the placeholder text
value before being displayed.
*/
@IBInspectable public private(set) lazy var placeholderLabel: UILabel = UILabel(frame: CGRectZero)
/// Placeholder textColor.
@IBInspectable public var placeholderColor: UIColor = MaterialColor.darkText.others {
didSet {
if !editing {
if let v: String = placeholder {
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderColor])
}
}
}
}
/// Placeholder active textColor.
@IBInspectable public var placeholderActiveColor: UIColor = MaterialColor.blue.base {
didSet {
if editing {
if let v: String = placeholder {
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderActiveColor])
}
}
tintColor = placeholderActiveColor
}
}
/// The detailLabel UILabel that is displayed.
@IBInspectable public private(set) lazy var detailLabel: UILabel = UILabel(frame: CGRectZero)
/// The detailLabel text value.
@IBInspectable public var detail: String? {
get {
return detailLabel.text
}
set(value) {
detailLabel.text = value
if let v: String = value {
detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailColor])
}
layoutDetailLabel()
}
}
/// Detail textColor.
@IBInspectable public var detailColor: UIColor = MaterialColor.darkText.others {
didSet {
if let v: String = detailLabel.text {
detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailColor])
}
}
}
/// Handles the textAlignment of the placeholderLabel.
public override var textAlignment: NSTextAlignment {
get {
return super.textAlignment
}
set(value) {
super.textAlignment = value
placeholderLabel.textAlignment = value
detailLabel.textAlignment = value
}
}
/// Enables the clearFlatButton.
@IBInspectable public var enableClearFlatButton: Bool {
get {
return nil != clearFlatButton
}
set(value) {
if value {
if nil == clearFlatButton {
let image: UIImage? = MaterialIcon.cm.clear
clearFlatButton = FlatButton(frame: CGRectZero)
clearFlatButton!.contentEdgeInsets = UIEdgeInsetsZero
clearFlatButton!.pulseColor = nil
clearFlatButton!.pulseScale = false
clearFlatButton!.tintColor = placeholderColor
clearFlatButton!.setImage(image, forState: .Normal)
clearButtonMode = .Never
rightViewMode = .WhileEditing
rightView = clearFlatButton
clearFlatButtonAutoHandle = clearFlatButtonAutoHandle ? true : false
}
} else {
clearFlatButton?.removeTarget(self, action: #selector(handleClearButton), forControlEvents: .TouchUpInside)
clearFlatButton = nil
}
}
}
/// Enables the automatic handling of the clearFlatButton.
@IBInspectable public var clearFlatButtonAutoHandle: Bool = true {
didSet {
clearFlatButton?.removeTarget(self, action: #selector(handleClearButton), forControlEvents: .TouchUpInside)
if clearFlatButtonAutoHandle {
clearFlatButton?.addTarget(self, action: #selector(handleClearButton), forControlEvents: .TouchUpInside)
}
}
}
/// Enables the visibilityFlatButton.
@IBInspectable public var enableVisibilityFlatButton: Bool {
get {
return nil != visibilityFlatButton
}
set(value) {
if value {
if nil == visibilityFlatButton {
let image: UIImage? = MaterialIcon.visibility
visibilityFlatButton = FlatButton(frame: CGRectZero)
visibilityFlatButton!.contentEdgeInsets = UIEdgeInsetsZero
visibilityFlatButton!.pulseColor = nil
visibilityFlatButton!.pulseScale = false
visibilityFlatButton!.tintColor = placeholderColor
visibilityFlatButton!.setImage(image, forState: .Normal)
secureTextEntry = true
clearButtonMode = .Never
rightViewMode = .WhileEditing
rightView = visibilityFlatButton
visibilityFlatButtonAutoHandle = visibilityFlatButtonAutoHandle ? true : false
}
} else {
visibilityFlatButton?.removeTarget(self, action: #selector(handleClearButton), forControlEvents: .TouchUpInside)
visibilityFlatButton = nil
}
}
}
/// Enables the automatic handling of the visibilityFlatButton.
@IBInspectable public var visibilityFlatButtonAutoHandle: Bool = true {
didSet {
visibilityFlatButton?.removeTarget(self, action: #selector(handleVisibilityButton), forControlEvents: .TouchUpInside)
if visibilityFlatButtonAutoHandle {
visibilityFlatButton?.addTarget(self, action: #selector(handleVisibilityButton), forControlEvents: .TouchUpInside)
}
}
}
/// A reference to the clearFlatButton.
public private(set) var clearFlatButton: FlatButton?
/// A reference to the visibilityFlatButton.
public private(set) var visibilityFlatButton: FlatButton?
/**
An initializer that initializes the object with a NSCoder object.
- Parameter aDecoder: A NSCoder instance.
*/
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
prepareView()
}
/**
An initializer that initializes the object with a CGRect object.
If AutoLayout is used, it is better to initilize the instance
using the init() initializer.
- Parameter frame: A CGRect instance.
*/
public override init(frame: CGRect) {
super.init(frame: frame)
prepareView()
}
/// A convenience initializer.
public convenience init() {
self.init(frame: CGRectZero)
}
public override func layoutSubviews() {
super.layoutSubviews()
layoutToSize()
}
public override func layoutSublayersOfLayer(layer: CALayer) {
super.layoutSublayersOfLayer(layer)
if self.layer == layer {
layoutDivider()
}
}
/// Default size when using AutoLayout.
public override func intrinsicContentSize() -> CGSize {
return CGSizeMake(width, 32)
}
/**
A method that accepts CAAnimation objects and executes them on the
view's backing layer.
- Parameter animation: A CAAnimation instance.
*/
public func animate(animation: CAAnimation) {
animation.delegate = self
if let a: CABasicAnimation = animation as? CABasicAnimation {
a.fromValue = (nil == layer.presentationLayer() ? layer : layer.presentationLayer() as! CALayer).valueForKeyPath(a.keyPath!)
}
if let a: CAPropertyAnimation = animation as? CAPropertyAnimation {
layer.addAnimation(a, forKey: a.keyPath!)
} else if let a: CAAnimationGroup = animation as? CAAnimationGroup {
layer.addAnimation(a, forKey: nil)
} else if let a: CATransition = animation as? CATransition {
layer.addAnimation(a, forKey: kCATransition)
}
}
/**
A delegation method that is executed when the backing layer starts
running an animation.
- Parameter anim: The currently running CAAnimation instance.
*/
public override func animationDidStart(anim: CAAnimation) {
(delegate as? MaterialAnimationDelegate)?.materialAnimationDidStart?(anim)
}
/**
A delegation method that is executed when the backing layer stops
running an animation.
- Parameter anim: The CAAnimation instance that stopped running.
- Parameter flag: A boolean that indicates if the animation stopped
because it was completed or interrupted. True if completed, false
if interrupted.
*/
public override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
if let a: CAPropertyAnimation = anim as? CAPropertyAnimation {
if let b: CABasicAnimation = a as? CABasicAnimation {
if let v: AnyObject = b.toValue {
if let k: String = b.keyPath {
layer.setValue(v, forKeyPath: k)
layer.removeAnimationForKey(k)
}
}
}
(delegate as? MaterialAnimationDelegate)?.materialAnimationDidStop?(anim, finished: flag)
} else if let a: CAAnimationGroup = anim as? CAAnimationGroup {
for x in a.animations! {
animationDidStop(x, finished: true)
}
}
}
/// Handles the text editing did begin state.
public func handleEditingDidBegin() {
dividerEditingDidBeginAnimation()
placeholderEditingDidBeginAnimation()
}
/// Handles the text editing did end state.
public func handleEditingDidEnd() {
dividerEditingDidEndAnimation()
placeholderEditingDidEndAnimation()
}
/// Handles the clearFlatButton TouchUpInside event.
public func handleClearButton() {
if false == delegate?.textFieldShouldClear?(self) {
return
}
text = nil
}
/// Handles the visibilityFlatButton TouchUpInside event.
public func handleVisibilityButton() {
secureTextEntry = !secureTextEntry
visibilityFlatButton?.tintColor = placeholderColor.colorWithAlphaComponent(secureTextEntry ? 0.38 : 0.54)
}
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method
to initialize property values and other setup operations.
The super.prepareView method should always be called immediately
when subclassing.
*/
public func prepareView() {
masksToBounds = false
borderStyle = .None
backgroundColor = nil
textColor = MaterialColor.darkText.primary
font = RobotoFont.regularWithSize(16)
prepareDivider()
preparePlaceholderLabel()
prepareDetailLabel()
prepareTargetHandlers()
}
/// Ensures that the components are sized correctly.
public func layoutToSize() {
if !animating {
layoutPlaceholderLabel()
layoutDetailLabel()
layoutClearFlatButton()
layoutVisibilityFlatButton()
}
}
/// Layout the divider.
public func layoutDivider() {
divider.frame = CGRectMake(0, height, width, editing ? dividerActiveHeight : dividerHeight)
}
/// Layout the placeholderLabel.
public func layoutPlaceholderLabel() {
if !editing && true == text?.isEmpty {
placeholderLabel.frame = bounds
} else if CGAffineTransformIsIdentity(placeholderLabel.transform) {
placeholderLabel.frame = bounds
placeholderLabel.transform = CGAffineTransformMakeScale(0.75, 0.75)
switch textAlignment {
case .Left, .Natural:
placeholderLabel.frame.origin.x = 0
case .Right:
placeholderLabel.frame.origin.x = width - placeholderLabel.frame.width
default:break
}
placeholderLabel.frame.origin.y = -placeholderLabel.frame.size.height
placeholderLabel.textColor = placeholderColor
} else {
switch textAlignment {
case .Left, .Natural:
placeholderLabel.frame.origin.x = 0
case .Right:
placeholderLabel.frame.origin.x = width - placeholderLabel.frame.width
case .Center:
placeholderLabel.center.x = width / 2
default:break
}
placeholderLabel.frame.size.width = width * 0.75
}
}
/// Layout the detailLabel.
public func layoutDetailLabel() {
var h: CGFloat = nil == detail ? 12 : detailLabel.font.stringSize(detail!, constrainedToWidth: Double(width)).height
detailLabel.frame = CGRectMake(0, divider.frame.origin.y + 8, width, h)
}
/// Layout the clearFlatButton.
public func layoutClearFlatButton() {
if let v: FlatButton = clearFlatButton {
if 0 < width && 0 < height {
v.frame = CGRectMake(width - height, 0, height, height)
}
}
}
/// Layout the visibilityFlatButton.
public func layoutVisibilityFlatButton() {
if let v: FlatButton = visibilityFlatButton {
if 0 < width && 0 < height {
v.frame = CGRectMake(width - height, 0, height, height)
}
}
}
/// The animation for the divider when editing begins.
public func dividerEditingDidBeginAnimation() {
divider.frame.size.height = dividerActiveHeight
divider.backgroundColor = nil == dividerActiveColor ? placeholderActiveColor.CGColor : dividerActiveColor!.CGColor
}
/// The animation for the divider when editing ends.
public func dividerEditingDidEndAnimation() {
divider.frame.size.height = dividerHeight
divider.backgroundColor = dividerColor.CGColor
}
/// The animation for the placeholder when editing begins.
public func placeholderEditingDidBeginAnimation() {
if CGAffineTransformIsIdentity(placeholderLabel.transform) {
animating = true
UIView.animateWithDuration(0.15, animations: { [weak self] in
if let v: MTextField = self {
v.placeholderLabel.transform = CGAffineTransformMakeScale(0.75, 0.75)
switch v.textAlignment {
case .Left, .Natural:
v.placeholderLabel.frame.origin.x = 0
case .Right:
v.placeholderLabel.frame.origin.x = v.width - v.placeholderLabel.frame.width
default:break
}
v.placeholderLabel.frame.origin.y = -v.placeholderLabel.frame.size.height
v.placeholderLabel.textColor = v.placeholderActiveColor
}
}) { [weak self] _ in
self?.animating = false
}
} else if editing {
placeholderLabel.textColor = placeholderActiveColor
}
}
/// The animation for the placeholder when editing ends.
public func placeholderEditingDidEndAnimation() {
if !CGAffineTransformIsIdentity(placeholderLabel.transform) && true == text?.isEmpty {
animating = true
UIView.animateWithDuration(0.15, animations: { [weak self] in
if let v: MTextField = self {
v.placeholderLabel.transform = CGAffineTransformIdentity
v.placeholderLabel.frame.origin.x = 0
v.placeholderLabel.frame.origin.y = 0
v.placeholderLabel.textColor = v.placeholderColor
}
}) { [weak self] _ in
self?.animating = false
}
} else if !editing {
placeholderLabel.textColor = placeholderColor
}
}
/// Prepares the divider.
private func prepareDivider() {
dividerColor = MaterialColor.darkText.dividers
layer.addSublayer(divider)
}
/// Prepares the placeholderLabel.
private func preparePlaceholderLabel() {
placeholderColor = MaterialColor.darkText.others
addSubview(placeholderLabel)
}
/// Prepares the detailLabel.
private func prepareDetailLabel() {
detailLabel.font = RobotoFont.regularWithSize(12)
detailColor = MaterialColor.darkText.others
addSubview(detailLabel)
}
/// Prepares the target handlers.
private func prepareTargetHandlers() {
addTarget(self, action: #selector(handleEditingDidBegin), forControlEvents: .EditingDidBegin)
addTarget(self, action: #selector(handleEditingDidEnd), forControlEvents: .EditingDidEnd)
}
}
\ No newline at end of file
...@@ -34,11 +34,7 @@ public protocol TextFieldDelegate : UITextFieldDelegate {} ...@@ -34,11 +34,7 @@ public protocol TextFieldDelegate : UITextFieldDelegate {}
@IBDesignable @IBDesignable
public class TextField : UITextField { public class TextField : UITextField {
/// A boolean that indicates if the titleLabel is animating. public private(set) var animating: Bool = false
private var titleLabelAnimating: Bool = false
/// A reference to the placeholder value.
private var placeholderText: String?
/** /**
This property is the same as clipsToBounds. It crops any of the view's This property is the same as clipsToBounds. It crops any of the view's
...@@ -100,310 +96,230 @@ public class TextField : UITextField { ...@@ -100,310 +96,230 @@ public class TextField : UITextField {
} }
} }
/// A property that accesses the backing layer's shadowColor. /// A property that accesses the layer.position property.
@IBInspectable public var shadowColor: UIColor? { @IBInspectable public var position: CGPoint {
didSet {
layer.shadowColor = shadowColor?.CGColor
}
}
/// A property that accesses the backing layer's shadowOffset.
@IBInspectable public var shadowOffset: CGSize {
get { get {
return layer.shadowOffset return layer.position
} }
set(value) { set(value) {
layer.shadowOffset = value layer.position = value
} }
} }
/// A property that accesses the backing layer's shadowOpacity. /// A property that accesses the layer.zPosition property.
@IBInspectable public var shadowOpacity: Float { @IBInspectable public var zPosition: CGFloat {
get { get {
return layer.shadowOpacity return layer.zPosition
} }
set(value) { set(value) {
layer.shadowOpacity = value layer.zPosition = value
} }
} }
/// A property that accesses the backing layer's shadowRadius. /// Reference to the divider.
@IBInspectable public var shadowRadius: CGFloat { public private(set) lazy var divider: CAShapeLayer = CAShapeLayer()
get {
return layer.shadowRadius
}
set(value) {
layer.shadowRadius = value
}
}
/// A property that accesses the backing layer's shadowPath. /// Divider height.
@IBInspectable public var shadowPath: CGPath? { @IBInspectable public var dividerHeight: CGFloat = 1
get {
return layer.shadowPath
}
set(value) {
layer.shadowPath = value
}
}
/// Enables automatic shadowPath sizing. /// Divider active state height.
@IBInspectable public var shadowPathAutoSizeEnabled: Bool = true { @IBInspectable public var dividerActiveHeight: CGFloat = 2
didSet {
if shadowPathAutoSizeEnabled {
layoutShadowPath()
} else {
shadowPath = nil
}
}
}
/**
A property that sets the shadowOffset, shadowOpacity, and shadowRadius
for the backing layer. This is the preferred method of setting depth
in order to maintain consitency across UI objects.
*/
public var depth: MaterialDepth = .None {
didSet {
let value: MaterialDepthType = MaterialDepthToValue(depth)
shadowOffset = value.offset
shadowOpacity = value.opacity
shadowRadius = value.radius
layoutShadowPath()
}
}
/// A property that sets the cornerRadius of the backing layer. /// Sets the divider.
public var cornerRadiusPreset: MaterialRadius = .None { @IBInspectable public var dividerColor: UIColor = MaterialColor.darkText.dividers {
didSet { didSet {
if let v: MaterialRadius = cornerRadiusPreset { if !editing {
cornerRadius = MaterialRadiusToValue(v) divider.backgroundColor = dividerColor.CGColor
} }
} }
} }
/// A property that accesses the layer.cornerRadius. /// Sets the divider.
@IBInspectable public var cornerRadius: CGFloat { @IBInspectable public var dividerActiveColor: UIColor? {
get {
return layer.cornerRadius
}
set(value) {
layer.cornerRadius = value
layoutShadowPath()
}
}
/// A preset property to set the borderWidth.
public var borderWidthPreset: MaterialBorder = .None {
didSet { didSet {
borderWidth = MaterialBorderToValue(borderWidthPreset) if let v: UIColor = dividerActiveColor {
} if editing {
} divider.backgroundColor = v.CGColor
}
/// A property that accesses the layer.borderWith. }
@IBInspectable public var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set(value) {
layer.borderWidth = value
} }
} }
/// A property that accesses the layer.borderColor property. /// The placeholderLabel font value.
@IBInspectable public var borderColor: UIColor? { @IBInspectable public override var font: UIFont? {
get { get {
return nil == layer.borderColor ? nil : UIColor(CGColor: layer.borderColor!) return placeholderLabel.font
} }
set(value) { set(value) {
layer.borderColor = value?.CGColor placeholderLabel.font = value
} }
} }
/// A property that accesses the layer.position property. /// The placeholderLabel text value.
@IBInspectable public var position: CGPoint { @IBInspectable public override var placeholder: String? {
get { get {
return layer.position return placeholderLabel.text
} }
set(value) { set(value) {
layer.position = value placeholderLabel.text = value
if let v: String = value {
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderColor])
}
} }
} }
/// A property that accesses the layer.zPosition property. /**
@IBInspectable public var zPosition: CGFloat { The placeholderLabel UILabel that is displayed when there is text. The
get { placeholderLabel text value is updated with the placeholder text
return layer.zPosition value before being displayed.
} */
set(value) { @IBInspectable public private(set) lazy var placeholderLabel: UILabel = UILabel(frame: CGRectZero)
layer.zPosition = value
}
}
/// Handle the clearButton manually. /// Placeholder textColor.
@IBInspectable public var clearButtonAutoHandleEnabled: Bool = true { @IBInspectable public var placeholderColor: UIColor = MaterialColor.darkText.others {
didSet { didSet {
clearButton.removeTarget(self, action: #selector(handleClearButton), forControlEvents: .TouchUpInside) if !editing {
if clearButtonAutoHandleEnabled { if let v: String = placeholder {
clearButton.addTarget(self, action: #selector(handleClearButton), forControlEvents: .TouchUpInside) placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderColor])
}
} }
} }
} }
/// Reference to the clearButton. /// Placeholder active textColor.
public private(set) var clearButton: FlatButton! @IBInspectable public var placeholderActiveColor: UIColor = MaterialColor.blue.base {
/// The bottom border layer.
public private(set) lazy var lineLayer: CAShapeLayer = CAShapeLayer()
/**
A property that sets the distance between the textField and
lineLayer.
*/
@IBInspectable public var lineLayerDistance: CGFloat = 4
/// The height of the line when not active.
@IBInspectable public var lineLayerThickness: CGFloat = 1
/// The height of the line when active.
@IBInspectable public var lineLayerActiveThickness: CGFloat = 2
/// The lineLayer color when inactive.
@IBInspectable public var lineLayerColor: UIColor? {
didSet { didSet {
lineLayer.backgroundColor = lineLayerColor?.CGColor if editing {
if let v: String = placeholder {
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderActiveColor])
}
}
tintColor = placeholderActiveColor
} }
} }
/// The lineLayer active color. /// The detailLabel UILabel that is displayed.
@IBInspectable public var lineLayerActiveColor: UIColor? @IBInspectable public private(set) lazy var detailLabel: UILabel = UILabel(frame: CGRectZero)
/// The lineLayer detail color when inactive.
@IBInspectable public var lineLayerDetailColor: UIColor?
/// The lineLayer detail active color. /// The detailLabel text value.
@IBInspectable public var lineLayerDetailActiveColor: UIColor? @IBInspectable public var detail: String? {
get {
/** return detailLabel.text
The title UILabel that is displayed when there is text. The }
titleLabel text value is updated with the placeholder text set(value) {
value before being displayed. detailLabel.text = value
*/ if let v: String = value {
@IBInspectable public private(set) var titleLabel: UILabel! detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailColor])
/// The color of the titleLabel text when the textField is not active.
@IBInspectable public var titleLabelColor: UIColor? {
didSet {
titleLabel.textColor = titleLabelColor
if nil == lineLayerColor {
lineLayerColor = titleLabelColor
} }
layoutDetailLabel()
} }
} }
/// The color of the titleLabel text when the textField is active. /// Detail textColor.
@IBInspectable public var titleLabelActiveColor: UIColor? { @IBInspectable public var detailColor: UIColor = MaterialColor.darkText.others {
didSet { didSet {
if nil == lineLayerActiveColor { if let v: String = detailLabel.text {
lineLayerActiveColor = titleLabelActiveColor detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailColor])
} }
tintColor = titleLabelActiveColor
} }
} }
/** /// Handles the textAlignment of the placeholderLabel.
A property that sets the distance between the textField and public override var textAlignment: NSTextAlignment {
titleLabel. get {
*/ return super.textAlignment
@IBInspectable public var titleLabelAnimationDistance: CGFloat = 4 }
set(value) {
/** super.textAlignment = value
The detail UILabel that is displayed when the detailLabelHidden property placeholderLabel.textAlignment = value
is set to false. detailLabel.textAlignment = value
*/ }
@IBInspectable public private(set) var detailLabel: UILabel! }
/** /// Enables the clearFlatButton.
The color of the detailLabel text when the detailLabelHidden property @IBInspectable public var enableClearFlatButton: Bool {
is set to false. get {
*/ return nil != clearFlatButton
@IBInspectable public var detailLabelActiveColor: UIColor? { }
didSet { set(value) {
if !detailLabelHidden { if value {
detailLabel.textColor = detailLabelActiveColor if nil == clearFlatButton {
if nil == lineLayerDetailActiveColor { let image: UIImage? = MaterialIcon.cm.clear
lineLayerDetailActiveColor = detailLabelActiveColor clearFlatButton = FlatButton(frame: CGRectZero)
clearFlatButton!.contentEdgeInsets = UIEdgeInsetsZero
clearFlatButton!.pulseColor = nil
clearFlatButton!.pulseScale = false
clearFlatButton!.tintColor = placeholderColor
clearFlatButton!.setImage(image, forState: .Normal)
clearFlatButton!.setImage(image, forState: .Highlighted)
clearButtonMode = .Never
rightViewMode = .WhileEditing
rightView = clearFlatButton
clearFlatButtonAutoHandle = clearFlatButtonAutoHandle ? true : false
} }
} else {
clearFlatButton?.removeTarget(self, action: #selector(handleClearButton), forControlEvents: .TouchUpInside)
clearFlatButton = nil
} }
} }
} }
/** /// Enables the automatic handling of the clearFlatButton.
A property that sets the distance between the textField and @IBInspectable public var clearFlatButtonAutoHandle: Bool = true {
detailLabel.
*/
@IBInspectable public var detailLabelAnimationDistance: CGFloat = 8
/**
A Boolean that indicates the detailLabel should hide
automatically when text changes.
*/
@IBInspectable public var detailLabelAutoHideEnabled: Bool = true
/// A boolean that indicates to hide or not hide the detailLabel.
@IBInspectable public var detailLabelHidden: Bool = true {
didSet { didSet {
if detailLabelHidden { clearFlatButton?.removeTarget(self, action: #selector(handleClearButton), forControlEvents: .TouchUpInside)
detailLabel.textColor = titleLabelColor if clearFlatButtonAutoHandle {
lineLayer.backgroundColor = (editing ? lineLayerActiveColor : lineLayerColor)?.CGColor clearFlatButton?.addTarget(self, action: #selector(handleClearButton), forControlEvents: .TouchUpInside)
hideDetailLabel()
} else {
detailLabel.textColor = detailLabelActiveColor
lineLayer.backgroundColor = (nil == lineLayerDetailActiveColor ? detailLabelActiveColor : lineLayerDetailActiveColor)?.CGColor
showDetailLabel()
} }
} }
} }
/// Sets the placeholder value. /// Enables the visibilityFlatButton.
@IBInspectable public override var placeholder: String? { @IBInspectable public var enableVisibilityFlatButton: Bool {
get { get {
return editing ? nil : placeholderText return nil != visibilityFlatButton
} }
set(value) { set(value) {
if let v: String = value { if value {
placeholderText = v if nil == visibilityFlatButton {
attributedPlaceholder = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderTextColor]) let image: UIImage? = MaterialIcon.visibility
} else { visibilityFlatButton = FlatButton(frame: CGRectZero)
super.placeholder = nil visibilityFlatButton!.contentEdgeInsets = UIEdgeInsetsZero
if !editing { visibilityFlatButton!.pulseColor = nil
placeholderText = nil visibilityFlatButton!.pulseScale = false
visibilityFlatButton!.tintColor = placeholderColor
visibilityFlatButton!.setImage(image, forState: .Normal)
visibilityFlatButton!.setImage(image, forState: .Highlighted)
visibilityFlatButton!.tintColor = placeholderColor.colorWithAlphaComponent(secureTextEntry ? 0.38 : 0.54)
secureTextEntry = true
clearButtonMode = .Never
rightViewMode = .WhileEditing
rightView = visibilityFlatButton
visibilityFlatButtonAutoHandle = visibilityFlatButtonAutoHandle ? true : false
} }
} } else {
if false == text?.isEmpty { visibilityFlatButton?.removeTarget(self, action: #selector(handleClearButton), forControlEvents: .TouchUpInside)
titleLabel.text = placeholderText visibilityFlatButton = nil
} }
} }
} }
/// Placeholder textColor. /// Enables the automatic handling of the visibilityFlatButton.
@IBInspectable public var placeholderTextColor: UIColor = MaterialColor.darkText.others { @IBInspectable public var visibilityFlatButtonAutoHandle: Bool = true {
didSet { didSet {
if let v: String = placeholder { visibilityFlatButton?.removeTarget(self, action: #selector(handleVisibilityButton), forControlEvents: .TouchUpInside)
attributedPlaceholder = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderTextColor]) if visibilityFlatButtonAutoHandle {
visibilityFlatButton?.addTarget(self, action: #selector(handleVisibilityButton), forControlEvents: .TouchUpInside)
} }
} }
} }
/// An override to the text property. /// A reference to the clearFlatButton.
@IBInspectable public override var text: String? { public private(set) var clearFlatButton: FlatButton?
didSet {
sendActionsForControlEvents(.ValueChanged) /// A reference to the visibilityFlatButton.
if nil != oldValue && true == text?.isEmpty && !editing { public private(set) var visibilityFlatButton: FlatButton?
handleEditingDidEnd()
}
}
}
/** /**
An initializer that initializes the object with a NSCoder object. An initializer that initializes the object with a NSCoder object.
...@@ -432,17 +348,21 @@ public class TextField : UITextField { ...@@ -432,17 +348,21 @@ public class TextField : UITextField {
public override func layoutSubviews() { public override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
layoutClearButton() layoutToSize()
} }
public override func layoutSublayersOfLayer(layer: CALayer) { public override func layoutSublayersOfLayer(layer: CALayer) {
super.layoutSublayersOfLayer(layer) super.layoutSublayersOfLayer(layer)
if self.layer == layer { if self.layer == layer {
layoutLineLayer() layoutDivider()
layoutShadowPath()
} }
} }
/// Default size when using AutoLayout.
public override func intrinsicContentSize() -> CGSize {
return CGSizeMake(width, 32)
}
/** /**
A method that accepts CAAnimation objects and executes them on the A method that accepts CAAnimation objects and executes them on the
view's backing layer. view's backing layer.
...@@ -497,6 +417,32 @@ public class TextField : UITextField { ...@@ -497,6 +417,32 @@ public class TextField : UITextField {
} }
} }
/// Handles the text editing did begin state.
public func handleEditingDidBegin() {
dividerEditingDidBeginAnimation()
placeholderEditingDidBeginAnimation()
}
/// Handles the text editing did end state.
public func handleEditingDidEnd() {
dividerEditingDidEndAnimation()
placeholderEditingDidEndAnimation()
}
/// Handles the clearFlatButton TouchUpInside event.
public func handleClearButton() {
if false == delegate?.textFieldShouldClear?(self) {
return
}
text = nil
}
/// Handles the visibilityFlatButton TouchUpInside event.
public func handleVisibilityButton() {
secureTextEntry = !secureTextEntry
visibilityFlatButton?.tintColor = visibilityFlatButton?.tintColor.colorWithAlphaComponent(secureTextEntry ? 0.38 : 0.54)
}
/** /**
Prepares the view instance when intialized. When subclassing, Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method it is recommended to override the prepareView method
...@@ -507,195 +453,162 @@ public class TextField : UITextField { ...@@ -507,195 +453,162 @@ public class TextField : UITextField {
public func prepareView() { public func prepareView() {
masksToBounds = false masksToBounds = false
borderStyle = .None borderStyle = .None
backgroundColor = MaterialColor.white backgroundColor = nil
textColor = MaterialColor.darkText.primary textColor = MaterialColor.darkText.primary
font = RobotoFont.regularWithSize(16) font = RobotoFont.regularWithSize(16)
prepareClearButton() prepareDivider()
prepareTitleLabel() preparePlaceholderLabel()
prepareLineLayer()
prepareDetailLabel() prepareDetailLabel()
addTarget(self, action: #selector(handleEditingDidBegin), forControlEvents: .EditingDidBegin) prepareTargetHandlers()
addTarget(self, action: #selector(handleEditingChanged), forControlEvents: .EditingChanged)
addTarget(self, action: #selector(handleEditingDidEnd), forControlEvents: .EditingDidEnd)
addTarget(self, action: #selector(handleValueChanged), forControlEvents: .ValueChanged)
} }
/// Handler for editing changed. /// Ensures that the components are sized correctly.
internal func handleEditingChanged() { public func layoutToSize() {
sendActionsForControlEvents(.ValueChanged) if !animating {
layoutPlaceholderLabel()
layoutDetailLabel()
layoutClearFlatButton()
layoutVisibilityFlatButton()
}
} }
/// Clears the textField text. /// Layout the divider.
internal func handleClearButton() { public func layoutDivider() {
if false == delegate?.textFieldShouldClear?(self) { divider.frame = CGRectMake(0, height, width, editing ? dividerActiveHeight : dividerHeight)
return
}
text = nil
} }
/// Ahdnler when text value changed. /// Layout the placeholderLabel.
internal func handleValueChanged() { public func layoutPlaceholderLabel() {
if detailLabelAutoHideEnabled && !detailLabelHidden { if !editing && true == text?.isEmpty {
detailLabelHidden = true placeholderLabel.frame = bounds
lineLayer.backgroundColor = (nil == lineLayerActiveColor ? titleLabelActiveColor : lineLayerActiveColor)?.CGColor } else if CGAffineTransformIsIdentity(placeholderLabel.transform) {
placeholderLabel.frame = bounds
placeholderLabel.transform = CGAffineTransformMakeScale(0.75, 0.75)
switch textAlignment {
case .Left, .Natural:
placeholderLabel.frame.origin.x = 0
case .Right:
placeholderLabel.frame.origin.x = width - placeholderLabel.frame.width
default:break
}
placeholderLabel.frame.origin.y = -placeholderLabel.frame.size.height
placeholderLabel.textColor = placeholderColor
} else {
switch textAlignment {
case .Left, .Natural:
placeholderLabel.frame.origin.x = 0
case .Right:
placeholderLabel.frame.origin.x = width - placeholderLabel.frame.width
case .Center:
placeholderLabel.center.x = width / 2
default:break
}
placeholderLabel.frame.size.width = width * 0.75
} }
} }
/// Handler for text editing began. /// Layout the detailLabel.
internal func handleEditingDidBegin() { public func layoutDetailLabel() {
showTitleLabel() var h: CGFloat = nil == detail ? 12 : detailLabel.font.stringSize(detail!, constrainedToWidth: Double(width)).height
placeholder = nil detailLabel.frame = CGRectMake(0, divider.frame.origin.y + 8, width, h)
titleLabel.textColor = titleLabelActiveColor
lineLayer.frame.size.height = lineLayerActiveThickness
lineLayer.backgroundColor = (detailLabelHidden ? nil == lineLayerActiveColor ? titleLabelActiveColor : lineLayerActiveColor : nil == lineLayerDetailActiveColor ? detailLabelActiveColor : lineLayerDetailActiveColor)?.CGColor
} }
/// Handler for text editing ended. /// Layout the clearFlatButton.
internal func handleEditingDidEnd() { public func layoutClearFlatButton() {
if true == text?.isEmpty && !titleLabelAnimating { if let v: FlatButton = clearFlatButton {
hideTitleLabel() if 0 < width && 0 < height {
v.frame = CGRectMake(width - height, 0, height, height)
}
} }
titleLabel.textColor = titleLabelColor
lineLayer.frame.size.height = lineLayerThickness
lineLayer.backgroundColor = (detailLabelHidden ? nil == lineLayerColor ? titleLabelColor : lineLayerColor : nil == lineLayerDetailColor ? detailLabelActiveColor : lineLayerDetailColor)?.CGColor
} }
/// Sets the shadow path. /// Layout the visibilityFlatButton.
internal func layoutShadowPath() { public func layoutVisibilityFlatButton() {
if shadowPathAutoSizeEnabled { if let v: FlatButton = visibilityFlatButton {
if .None == depth { if 0 < width && 0 < height {
shadowPath = nil v.frame = CGRectMake(width - height, 0, height, height)
} else if nil == shadowPath {
shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).CGPath
} else {
animate(MaterialAnimation.shadowPath(UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).CGPath, duration: 0))
} }
} }
} }
/// Prepares the titleLabel. /// The animation for the divider when editing begins.
private func prepareTitleLabel() { public func dividerEditingDidBeginAnimation() {
titleLabel = UILabel(frame: CGRectZero) divider.frame.size.height = dividerActiveHeight
titleLabel.hidden = true divider.backgroundColor = nil == dividerActiveColor ? placeholderActiveColor.CGColor : dividerActiveColor!.CGColor
titleLabel.font = RobotoFont.mediumWithSize(12)
addSubview(titleLabel)
titleLabelColor = placeholderTextColor
titleLabelActiveColor = MaterialColor.blue.accent3
if false == text?.isEmpty {
showTitleLabel()
}
} }
/// Prepares the detailLabel. /// The animation for the divider when editing ends.
private func prepareDetailLabel() { public func dividerEditingDidEndAnimation() {
detailLabel = UILabel(frame: CGRectZero) divider.frame.size.height = dividerHeight
detailLabel.hidden = true divider.backgroundColor = dividerColor.CGColor
addSubview(detailLabel)
if detailLabelHidden {
detailLabel.alpha = 0
} else {
showDetailLabel()
}
} }
/// Prepares the lineLayer. /// The animation for the placeholder when editing begins.
private func prepareLineLayer() { public func placeholderEditingDidBeginAnimation() {
layoutLineLayer() if CGAffineTransformIsIdentity(placeholderLabel.transform) {
layer.addSublayer(lineLayer) animating = true
UIView.animateWithDuration(0.15, animations: { [weak self] in
if let v: TextField = self {
v.placeholderLabel.transform = CGAffineTransformMakeScale(0.75, 0.75)
switch v.textAlignment {
case .Left, .Natural:
v.placeholderLabel.frame.origin.x = 0
case .Right:
v.placeholderLabel.frame.origin.x = v.width - v.placeholderLabel.frame.width
default:break
}
v.placeholderLabel.frame.origin.y = -v.placeholderLabel.frame.size.height
v.placeholderLabel.textColor = v.placeholderActiveColor
}
}) { [weak self] _ in
self?.animating = false
}
} else if editing {
placeholderLabel.textColor = placeholderActiveColor
}
} }
/// Layout the lineLayer. /// The animation for the placeholder when editing ends.
private func layoutLineLayer() { public func placeholderEditingDidEndAnimation() {
let h: CGFloat = lineLayerActiveThickness == lineLayer.frame.height ? lineLayerActiveThickness : lineLayerThickness if !CGAffineTransformIsIdentity(placeholderLabel.transform) && true == text?.isEmpty {
lineLayer.frame = CGRectMake(0, bounds.height + lineLayerDistance, bounds.width, h) animating = true
UIView.animateWithDuration(0.15, animations: { [weak self] in
if let v: TextField = self {
v.placeholderLabel.transform = CGAffineTransformIdentity
v.placeholderLabel.frame.origin.x = 0
v.placeholderLabel.frame.origin.y = 0
v.placeholderLabel.textColor = v.placeholderColor
}
}) { [weak self] _ in
self?.animating = false
}
} else if !editing {
placeholderLabel.textColor = placeholderColor
}
} }
/// Prepares the clearButton. /// Prepares the divider.
private func prepareClearButton() { private func prepareDivider() {
let image: UIImage? = MaterialIcon.cm.close dividerColor = MaterialColor.darkText.dividers
clearButton = FlatButton() layer.addSublayer(divider)
clearButton.contentEdgeInsets = UIEdgeInsetsZero
clearButton.pulseColor = MaterialColor.black
clearButton.pulseOpacity = 0.12
clearButton.pulseScale = false
clearButton.tintColor = placeholderTextColor
clearButton.setImage(image, forState: .Normal)
clearButton.setImage(image, forState: .Highlighted)
clearButtonAutoHandleEnabled = true
clearButtonMode = .Never
rightViewMode = .WhileEditing
rightView = clearButton
} }
/// Layout the clearButton. /// Prepares the placeholderLabel.
private func layoutClearButton() { private func preparePlaceholderLabel() {
if 0 < width && 0 < height { placeholderColor = MaterialColor.darkText.others
// clearButton.frame = CGRectMake(width - height, 0, height, height) addSubview(placeholderLabel)
}
} }
/// Shows and animates the titleLabel property. /// Prepares the detailLabel.
private func showTitleLabel() { private func prepareDetailLabel() {
if titleLabel.hidden { detailLabel.font = RobotoFont.regularWithSize(12)
let h: CGFloat = ceil(titleLabel.font.lineHeight) detailColor = MaterialColor.darkText.others
titleLabel.frame = bounds addSubview(detailLabel)
titleLabel.font = font
titleLabel.text = placeholderText
titleLabel.hidden = false
UIView.animateWithDuration(0.15, animations: { [weak self] in
if let s: TextField = self {
s.titleLabel.transform = CGAffineTransformScale(s.titleLabel.transform, 0.75, 0.75)
s.titleLabel.frame = CGRectMake(0, -(s.titleLabelAnimationDistance + h), s.bounds.width, h)
}
})
}
}
/// Hides and animates the titleLabel property.
private func hideTitleLabel() {
titleLabelAnimating = true
UIView.animateWithDuration(0.15, animations: { [weak self] in
if let s: TextField = self {
s.titleLabel.transform = CGAffineTransformIdentity
s.titleLabel.frame = s.bounds
}
}) { [weak self] _ in
if let s: TextField = self {
s.titleLabel.hidden = true
s.placeholder = s.placeholderText
s.titleLabelAnimating = false
}
}
}
/// Shows and animates the detailLabel property.
private func showDetailLabel() {
if detailLabel.hidden {
let h: CGFloat = ceil(detailLabel.font.lineHeight)
detailLabel.frame = CGRectMake(0, bounds.height + lineLayerDistance, bounds.width, h)
detailLabel.hidden = false
UIView.animateWithDuration(0.15, animations: { [weak self] in
if let s: TextField = self {
s.detailLabel.frame.origin.y = s.frame.height + s.lineLayerDistance + s.detailLabelAnimationDistance
s.detailLabel.alpha = 1
}
})
}
} }
/// Hides and animates the detailLabel property. /// Prepares the target handlers.
private func hideDetailLabel() { private func prepareTargetHandlers() {
UIView.animateWithDuration(0.15, animations: { [weak self] in addTarget(self, action: #selector(handleEditingDidBegin), forControlEvents: .EditingDidBegin)
if let s: TextField = self { addTarget(self, action: #selector(handleEditingDidEnd), forControlEvents: .EditingDidEnd)
s.detailLabel.alpha = 0
s.detailLabel.frame.origin.y -= s.detailLabelAnimationDistance
}
}) { [weak self] _ in
if let s: TextField = self {
s.detailLabel.hidden = true
}
}
} }
} }
\ No newline at end of file
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