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
/// Sets the divider.
@IBInspectable public var dividerColor: UIColor = MaterialColor.darkText.dividers {
didSet { didSet {
if shadowPathAutoSizeEnabled { if !editing {
layoutShadowPath() divider.backgroundColor = dividerColor.CGColor
} else {
shadowPath = nil
} }
} }
} }
/** /// Sets the divider.
A property that sets the shadowOffset, shadowOpacity, and shadowRadius @IBInspectable public var dividerActiveColor: UIColor? {
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 { didSet {
let value: MaterialDepthType = MaterialDepthToValue(depth) if let v: UIColor = dividerActiveColor {
shadowOffset = value.offset if editing {
shadowOpacity = value.opacity divider.backgroundColor = v.CGColor
shadowRadius = value.radius
layoutShadowPath()
}
} }
/// A property that sets the cornerRadius of the backing layer.
public var cornerRadiusPreset: MaterialRadius = .None {
didSet {
if let v: MaterialRadius = cornerRadiusPreset {
cornerRadius = MaterialRadiusToValue(v)
} }
} }
} }
/// A property that accesses the layer.cornerRadius. /// The placeholderLabel font value.
@IBInspectable public var cornerRadius: CGFloat { @IBInspectable public override var font: UIFont? {
get { get {
return layer.cornerRadius return placeholderLabel.font
} }
set(value) { set(value) {
layer.cornerRadius = value placeholderLabel.font = value
layoutShadowPath()
}
}
/// A preset property to set the borderWidth.
public var borderWidthPreset: MaterialBorder = .None {
didSet {
borderWidth = MaterialBorderToValue(borderWidthPreset)
} }
} }
/// A property that accesses the layer.borderWith. /// The placeholderLabel text value.
@IBInspectable public var borderWidth: CGFloat { @IBInspectable public override var placeholder: String? {
get { get {
return layer.borderWidth return placeholderLabel.text
} }
set(value) { set(value) {
layer.borderWidth = value placeholderLabel.text = value
} if let v: String = value {
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderColor])
} }
/// A property that accesses the layer.borderColor property.
@IBInspectable public var borderColor: UIColor? {
get {
return nil == layer.borderColor ? nil : UIColor(CGColor: layer.borderColor!)
}
set(value) {
layer.borderColor = value?.CGColor
} }
} }
/// A property that accesses the layer.position property. /**
@IBInspectable public var position: CGPoint { The placeholderLabel UILabel that is displayed when there is text. The
get { placeholderLabel text value is updated with the placeholder text
return layer.position value before being displayed.
} */
set(value) { @IBInspectable public private(set) lazy var placeholderLabel: UILabel = UILabel(frame: CGRectZero)
layer.position = value
}
}
/// A property that accesses the layer.zPosition property. /// Placeholder textColor.
@IBInspectable public var zPosition: CGFloat { @IBInspectable public var placeholderColor: UIColor = MaterialColor.darkText.others {
get { didSet {
return layer.zPosition if !editing {
if let v: String = placeholder {
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderColor])
}
} }
set(value) {
layer.zPosition = value
} }
} }
/// Handle the clearButton manually. /// Placeholder active textColor.
@IBInspectable public var clearButtonAutoHandleEnabled: Bool = true { @IBInspectable public var placeholderActiveColor: UIColor = MaterialColor.blue.base {
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: placeholderActiveColor])
} }
} }
tintColor = placeholderActiveColor
}
} }
/// Reference to the clearButton. /// The detailLabel UILabel that is displayed.
public private(set) var clearButton: FlatButton! @IBInspectable public private(set) lazy var detailLabel: UILabel = UILabel(frame: CGRectZero)
/// 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. /// The detailLabel text value.
@IBInspectable public var lineLayerColor: UIColor? { @IBInspectable public var detail: String? {
didSet { get {
lineLayer.backgroundColor = lineLayerColor?.CGColor return detailLabel.text
}
set(value) {
detailLabel.text = value
if let v: String = value {
detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailColor])
}
layoutDetailLabel()
} }
} }
/// The lineLayer active color. /// Detail textColor.
@IBInspectable public var lineLayerActiveColor: UIColor? @IBInspectable public var detailColor: UIColor = MaterialColor.darkText.others {
/// The lineLayer detail color when inactive.
@IBInspectable public var lineLayerDetailColor: UIColor?
/// The lineLayer detail active color.
@IBInspectable public var lineLayerDetailActiveColor: UIColor?
/**
The title UILabel that is displayed when there is text. The
titleLabel text value is updated with the placeholder text
value before being displayed.
*/
@IBInspectable public private(set) var titleLabel: UILabel!
/// The color of the titleLabel text when the textField is not active.
@IBInspectable public var titleLabelColor: UIColor? {
didSet { didSet {
titleLabel.textColor = titleLabelColor if let v: String = detailLabel.text {
if nil == lineLayerColor { detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailColor])
lineLayerColor = titleLabelColor
} }
} }
} }
/// The color of the titleLabel text when the textField is active. /// Handles the textAlignment of the placeholderLabel.
@IBInspectable public var titleLabelActiveColor: UIColor? { public override var textAlignment: NSTextAlignment {
didSet { get {
if nil == lineLayerActiveColor { return super.textAlignment
lineLayerActiveColor = titleLabelActiveColor
} }
tintColor = titleLabelActiveColor set(value) {
super.textAlignment = value
placeholderLabel.textAlignment = value
detailLabel.textAlignment = value
} }
} }
/** /// Enables the clearFlatButton.
A property that sets the distance between the textField and @IBInspectable public var enableClearFlatButton: Bool {
titleLabel. get {
*/ return nil != clearFlatButton
@IBInspectable public var titleLabelAnimationDistance: CGFloat = 4
/**
The detail UILabel that is displayed when the detailLabelHidden property
is set to false.
*/
@IBInspectable public private(set) var detailLabel: UILabel!
/**
The color of the detailLabel text when the detailLabelHidden property
is set to false.
*/
@IBInspectable public var detailLabelActiveColor: UIColor? {
didSet {
if !detailLabelHidden {
detailLabel.textColor = detailLabelActiveColor
if nil == lineLayerDetailActiveColor {
lineLayerDetailActiveColor = detailLabelActiveColor
} }
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)
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
} }
if false == text?.isEmpty { } else {
titleLabel.text = placeholderText visibilityFlatButton?.removeTarget(self, action: #selector(handleClearButton), forControlEvents: .TouchUpInside)
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.
internal func handleEditingChanged() {
sendActionsForControlEvents(.ValueChanged)
}
/// Clears the textField text.
internal func handleClearButton() {
if false == delegate?.textFieldShouldClear?(self) {
return
}
text = nil
} }
/// Ahdnler when text value changed. /// Ensures that the components are sized correctly.
internal func handleValueChanged() { public func layoutToSize() {
if detailLabelAutoHideEnabled && !detailLabelHidden { if !animating {
detailLabelHidden = true layoutPlaceholderLabel()
lineLayer.backgroundColor = (nil == lineLayerActiveColor ? titleLabelActiveColor : lineLayerActiveColor)?.CGColor layoutDetailLabel()
layoutClearFlatButton()
layoutVisibilityFlatButton()
} }
} }
/// Handler for text editing began. /// Layout the divider.
internal func handleEditingDidBegin() { public func layoutDivider() {
showTitleLabel() divider.frame = CGRectMake(0, height, width, editing ? dividerActiveHeight : dividerHeight)
placeholder = nil
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 placeholderLabel.
internal func handleEditingDidEnd() { public func layoutPlaceholderLabel() {
if true == text?.isEmpty && !titleLabelAnimating { if !editing && true == text?.isEmpty {
hideTitleLabel() 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
} }
titleLabel.textColor = titleLabelColor placeholderLabel.frame.origin.y = -placeholderLabel.frame.size.height
lineLayer.frame.size.height = lineLayerThickness placeholderLabel.textColor = placeholderColor
lineLayer.backgroundColor = (detailLabelHidden ? nil == lineLayerColor ? titleLabelColor : lineLayerColor : nil == lineLayerDetailColor ? detailLabelActiveColor : lineLayerDetailColor)?.CGColor
}
/// Sets the shadow path.
internal func layoutShadowPath() {
if shadowPathAutoSizeEnabled {
if .None == depth {
shadowPath = nil
} else if nil == shadowPath {
shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).CGPath
} else { } else {
animate(MaterialAnimation.shadowPath(UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).CGPath, duration: 0)) 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
} }
} }
/// Prepares the titleLabel. /// Layout the detailLabel.
private func prepareTitleLabel() { public func layoutDetailLabel() {
titleLabel = UILabel(frame: CGRectZero) var h: CGFloat = nil == detail ? 12 : detailLabel.font.stringSize(detail!, constrainedToWidth: Double(width)).height
titleLabel.hidden = true detailLabel.frame = CGRectMake(0, divider.frame.origin.y + 8, width, h)
titleLabel.font = RobotoFont.mediumWithSize(12)
addSubview(titleLabel)
titleLabelColor = placeholderTextColor
titleLabelActiveColor = MaterialColor.blue.accent3
if false == text?.isEmpty {
showTitleLabel()
}
} }
/// Prepares the detailLabel. /// Layout the clearFlatButton.
private func prepareDetailLabel() { public func layoutClearFlatButton() {
detailLabel = UILabel(frame: CGRectZero) if let v: FlatButton = clearFlatButton {
detailLabel.hidden = true if 0 < width && 0 < height {
addSubview(detailLabel) v.frame = CGRectMake(width - height, 0, height, height)
if detailLabelHidden {
detailLabel.alpha = 0
} else {
showDetailLabel()
}
}
/// Prepares the lineLayer.
private func prepareLineLayer() {
layoutLineLayer()
layer.addSublayer(lineLayer)
} }
/// Layout the lineLayer.
private func layoutLineLayer() {
let h: CGFloat = lineLayerActiveThickness == lineLayer.frame.height ? lineLayerActiveThickness : lineLayerThickness
lineLayer.frame = CGRectMake(0, bounds.height + lineLayerDistance, bounds.width, h)
} }
/// Prepares the clearButton.
private func prepareClearButton() {
let image: UIImage? = MaterialIcon.cm.close
clearButton = FlatButton()
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. /// Layout the visibilityFlatButton.
private func layoutClearButton() { public func layoutVisibilityFlatButton() {
if let v: FlatButton = visibilityFlatButton {
if 0 < width && 0 < height { if 0 < width && 0 < height {
// clearButton.frame = CGRectMake(width - height, 0, height, height) v.frame = CGRectMake(width - height, 0, height, height)
} }
} }
/// Shows and animates the titleLabel property.
private func showTitleLabel() {
if titleLabel.hidden {
let h: CGFloat = ceil(titleLabel.font.lineHeight)
titleLabel.frame = bounds
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)
} }
})
/// 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
} }
/// Hides and animates the titleLabel property. /// The animation for the placeholder when editing begins.
private func hideTitleLabel() { public func placeholderEditingDidBeginAnimation() {
titleLabelAnimating = true if CGAffineTransformIsIdentity(placeholderLabel.transform) {
animating = true
UIView.animateWithDuration(0.15, animations: { [weak self] in UIView.animateWithDuration(0.15, animations: { [weak self] in
if let s: TextField = self { if let v: TextField = self {
s.titleLabel.transform = CGAffineTransformIdentity v.placeholderLabel.transform = CGAffineTransformMakeScale(0.75, 0.75)
s.titleLabel.frame = s.bounds 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 }) { [weak self] _ in
if let s: TextField = self { self?.animating = false
s.titleLabel.hidden = true
s.placeholder = s.placeholderText
s.titleLabelAnimating = false
} }
} else if editing {
placeholderLabel.textColor = placeholderActiveColor
} }
} }
/// Shows and animates the detailLabel property. /// The animation for the placeholder when editing ends.
private func showDetailLabel() { public func placeholderEditingDidEndAnimation() {
if detailLabel.hidden { if !CGAffineTransformIsIdentity(placeholderLabel.transform) && true == text?.isEmpty {
let h: CGFloat = ceil(detailLabel.font.lineHeight) animating = true
detailLabel.frame = CGRectMake(0, bounds.height + lineLayerDistance, bounds.width, h)
detailLabel.hidden = false
UIView.animateWithDuration(0.15, animations: { [weak self] in UIView.animateWithDuration(0.15, animations: { [weak self] in
if let s: TextField = self { if let v: TextField = self {
s.detailLabel.frame.origin.y = s.frame.height + s.lineLayerDistance + s.detailLabelAnimationDistance v.placeholderLabel.transform = CGAffineTransformIdentity
s.detailLabel.alpha = 1 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
} }
} }
/// Hides and animates the detailLabel property. /// Prepares the divider.
private func hideDetailLabel() { private func prepareDivider() {
UIView.animateWithDuration(0.15, animations: { [weak self] in dividerColor = MaterialColor.darkText.dividers
if let s: TextField = self { layer.addSublayer(divider)
s.detailLabel.alpha = 0
s.detailLabel.frame.origin.y -= s.detailLabelAnimationDistance
} }
}) { [weak self] _ in
if let s: TextField = self { /// Prepares the placeholderLabel.
s.detailLabel.hidden = true 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
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