Commit 23dde70e by Daniel Dahan

temporary class MTextField added for new TextField

parent a8c2438b
......@@ -125,8 +125,7 @@ extension ViewController: MaterialCollectionViewDataSource {
textField.font = RobotoFont.regularWithSize(20)
textField.textColor = MaterialColor.black
textField.titleLabel = UILabel()
textField.titleLabel!.font = RobotoFont.mediumWithSize(12)
textField.titleLabel.font = RobotoFont.mediumWithSize(12)
textField.titleLabelColor = MaterialColor.grey.base
textField.titleLabelActiveColor = MaterialColor.blue.accent3
......@@ -135,10 +134,8 @@ extension ViewController: MaterialCollectionViewDataSource {
Used to display the error message, which is displayed when
the user presses the 'return' key.
*/
let detailLabel: UILabel = UILabel()
detailLabel.text = "detail text..."
textField.detailLabel = detailLabel
textField.detailLabel!.font = RobotoFont.mediumWithSize(12)
textField.detailLabel.text = "detail text..."
textField.detailLabel.font = RobotoFont.mediumWithSize(12)
textField.detailLabelActiveColor = MaterialColor.red.accent3
// textField.detailLabelAutoHideEnabled = false // Uncomment this line to have manual hiding.
......
......@@ -7,6 +7,8 @@
objects = {
/* Begin PBXBuildFile section */
961776711CCE75EB0091B4F3 /* Material.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 961776701CCE75EB0091B4F3 /* Material.framework */; };
961776721CCE75EB0091B4F3 /* Material.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 961776701CCE75EB0091B4F3 /* Material.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
966F57A11C226BAA009185B7 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 966F57A01C226BAA009185B7 /* AppDelegate.swift */; };
966F57A31C226BAA009185B7 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 966F57A21C226BAA009185B7 /* ViewController.swift */; };
966F57A81C226BAA009185B7 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 966F57A71C226BAA009185B7 /* Assets.xcassets */; };
......@@ -20,6 +22,7 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
961776721CCE75EB0091B4F3 /* Material.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
......@@ -27,6 +30,7 @@
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
961776701CCE75EB0091B4F3 /* Material.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; name = Material.framework; path = "/Users/danieldahan/Library/Developer/Xcode/DerivedData/Material-bshwisidfobstlbomegqyybbrygh/Build/Products/Debug-iphoneos/Material.framework"; sourceTree = "<absolute>"; };
966F579D1C226BAA009185B7 /* TextField.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TextField.app; sourceTree = BUILT_PRODUCTS_DIR; };
966F57A01C226BAA009185B7 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
966F57A21C226BAA009185B7 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
......@@ -40,6 +44,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
961776711CCE75EB0091B4F3 /* Material.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......@@ -49,6 +54,7 @@
966F57941C226BAA009185B7 = {
isa = PBXGroup;
children = (
961776701CCE75EB0091B4F3 /* Material.framework */,
966F579F1C226BAA009185B7 /* TextField */,
966F579E1C226BAA009185B7 /* Products */,
);
......
......@@ -38,8 +38,57 @@ under the TextField.
import UIKit
import Material
//extension TextField {
//
// func setDefaultLabelSpecs(titleColor titleColor: UIColor, detailColor: UIColor) {
// // ref: https://www.google.com/design/spec/components/text-fields.html#text-fields-labels
// // ref: https://www.google.com/design/spec/patterns/errors.html#errors-user-input-errors
// // ref: https://www.google.com/design/spec/layout/metrics-keylines.html#metrics-keylines-touch-target-size
// assert(height == 80, "Height must be 80, based on Material design spec.")
//
// backgroundColor = nil
//
// let derivedDetailLabelHeight: CGFloat = 15
// let paddingAboveAndBelowErrorText: CGFloat = 4
//
// detailLabel.font = RobotoFont.regularWithSize(12)
// detailLabelActiveColor = detailColor
// detailLabelAnimationDistance = lineLayerThickness + paddingAboveAndBelowErrorText
//
// lineLayerActiveColor = titleColor
// lineLayerColor = MaterialColor.darkText.dividers
// lineLayerDistance = 0 - (derivedDetailLabelHeight + (paddingAboveAndBelowErrorText * 2) + lineLayerThickness)
//
// let derivedTitleLabelHeight: CGFloat = 20
// let paddingAboveLabelText: CGFloat = 8
//
// titleLabelActiveColor = lineLayerActiveColor
// titleLabelAnimationDistance = 0 - (derivedTitleLabelHeight + paddingAboveLabelText)
//
// if let clearButton = clearButton {
// let touchTargetHeight: CGFloat = 48
// let spacing = (height - touchTargetHeight) / 2
// let origin = CGPoint(x: width - touchTargetHeight - spacing, y: spacing)
// let size = CGSize(width: touchTargetHeight, height: touchTargetHeight)
//
// clearButton.frame = CGRect(origin: origin, size: size)
//
// clearButton.contentHorizontalAlignment = .Right
//
// // Use PDF for better rendering
// let clearImage = UIImage(named: "Material/Navigation/Close")
// clearButton.setImage(clearImage, forState: .Normal)
// clearButton.setImage(clearImage, forState: .Highlighted)
//
// // Hide it since it bleeds over the lineLayer, and doesn't look good
// // with the horizontal alignment.
// clearButton.pulseOpacity = 0
// }
// }
//}
class ViewController: UIViewController, TextFieldDelegate {
private var nameField: TextField!
private var nameField: MTextField!
private var emailField: TextField!
override func viewDidLoad() {
......@@ -56,14 +105,16 @@ class ViewController: UIViewController, TextFieldDelegate {
/// Prepares the name TextField.
private func prepareNameField() {
nameField = TextField()
nameField = MTextField()
nameField.placeholder = "Name"
nameField.detail = "Error, incorrect password. yy ypp ggg"
nameField.placeholderTextColor = MaterialColor.green.base
view.addSubview(nameField)
nameField.translatesAutoresizingMaskIntoConstraints = false
MaterialLayout.alignFromTop(view, child: nameField, top: 100)
MaterialLayout.alignToParentHorizontally(view, child: nameField, left: 20, right: 20)
print(nameField)
}
/// Prepares the email TextField.
......@@ -73,6 +124,7 @@ class ViewController: UIViewController, TextFieldDelegate {
emailField = TextField(frame: CGRectMake(x, 200, w, 24))
emailField.placeholder = "Email"
emailField.delegate = self
emailField.text = "Hello World"
/*
Used to display the error message, which is displayed when
......
......@@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
96334EF61C8B84660083986B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 96334EF51C8B84660083986B /* Assets.xcassets */; };
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 */; };
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 */; };
......@@ -196,6 +197,7 @@
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; };
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; };
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>"; };
......@@ -580,6 +582,7 @@
96BCB79C1CB40DC500C806FE /* TextField.swift */,
96BCB79D1CB40DC500C806FE /* TextStorage.swift */,
96BCB79E1CB40DC500C806FE /* TextView.swift */,
9658F1E41CD0229C00B902C1 /* MTextField.swift */,
);
name = Text;
sourceTree = "<group>";
......@@ -882,6 +885,7 @@
96BCB7D11CB40DC500C806FE /* MaterialSwitch.swift in Sources */,
96BCB7BA1CB40DC500C806FE /* MaterialCollectionView.swift in Sources */,
96BCB7A31CB40DC500C806FE /* CapturePreview.swift in Sources */,
9658F1E51CD0229D00B902C1 /* MTextField.swift in Sources */,
96BCB7BC1CB40DC500C806FE /* MaterialCollectionViewDataSource.swift in Sources */,
96BCB7C71CB40DC500C806FE /* MaterialKeyframeAnimation.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 {
/**
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) var divider: CAShapeLayer!
@IBInspectable public override var placeholder: String? {
get {
return placeholderLabel.text
}
set(value) {
placeholderLabel.text = value
}
}
/**
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) var placeholderLabel: UILabel!
/// Placeholder textColor.
@IBInspectable public var placeholderTextColor: UIColor = MaterialColor.darkText.others {
didSet {
if let v: String = placeholder {
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderTextColor])
}
}
}
/// The detailLabel UILabel that is displayed.
@IBInspectable public private(set) var detailLabel: UILabel!
@IBInspectable public var detail: String? {
didSet {
detailLabel.text = detail
}
}
/**
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()
if !editing {
layoutIfNeeded()
layoutPlaceholderLabel()
layoutDetailLabel()
}
}
public override func layoutSublayersOfLayer(layer: CALayer) {
super.layoutSublayersOfLayer(layer)
if self.layer == layer {
if !editing {
layoutIfNeeded()
layoutDivider()
}
}
}
/**
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()
}
/**
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()
}
/// Layout the divider.
public func layoutDivider() {
divider.frame = CGRectMake(0, height + 8, width, 1)
}
/// Layout the placeholderLabel.
public func layoutPlaceholderLabel() {
if true == text?.isEmpty {
placeholderLabel.frame = bounds
}
}
/// Layout the detailLabel.
public func layoutDetailLabel() {
detailLabel.frame = CGRectMake(0, height + 16, width, 12)
detailLabel.sizeToFit()
}
/// The animation for the divider when editing begins.
public func dividerEditingDidBeginAnimation() {
divider.frame.size.height = 2
divider.backgroundColor = MaterialColor.blue.base.CGColor
}
/// The animation for the divider when editing ends.
public func dividerEditingDidEndAnimation() {
divider.frame.size.height = 1
divider.backgroundColor = MaterialColor.darkText.dividers.CGColor
}
/// The animation for the placeholder when editing begins.
public func placeholderEditingDidBeginAnimation() {
placeholderLabel.textColor = placeholderTextColor
if true == text?.isEmpty {
UIView.animateWithDuration(0.15, animations: { [weak self] in
if let s: MTextField = self {
s.placeholderLabel.transform = CGAffineTransformScale(s.placeholderLabel.transform, 0.75, 0.75)
s.placeholderLabel.frame = CGRectMake(0, -16, s.bounds.width, 12)
}
})
}
}
/// The animation for the placeholder when editing ends.
public func placeholderEditingDidEndAnimation() {
if true == text?.isEmpty {
UIView.animateWithDuration(0.15, animations: { [weak self] in
if let s: MTextField = self {
s.placeholderLabel.transform = CGAffineTransformIdentity
s.placeholderLabel.frame = s.bounds
}
})
}
}
/// Prepares the divider.
private func prepareDivider() {
divider = CAShapeLayer()
divider.backgroundColor = MaterialColor.darkText.dividers.CGColor
layer.addSublayer(divider)
}
/// Prepares the placeholderLabel.
private func preparePlaceholderLabel() {
placeholderLabel = UILabel()
addSubview(placeholderLabel)
}
/// Prepares the detailLabel.
private func prepareDetailLabel() {
detailLabel = UILabel()
detailLabel.font = RobotoFont.regularWithSize(12)
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
......@@ -64,9 +64,7 @@ public func MaterialAnimationFillModeToValue(mode: MaterialAnimationFillMode) ->
public typealias MaterialAnimationDelayCancelBlock = (cancel : Bool) -> Void
public struct MaterialAnimation {
/**
:name: delay
*/
/// Delay helper method.
public static func delay(time: NSTimeInterval, completion: ()-> Void) -> MaterialAnimationDelayCancelBlock? {
func dispatch_later(completion: ()-> Void) {
......@@ -109,7 +107,7 @@ public struct MaterialAnimation {
/**
:name: animateWithDuration
*/
public static func animateWithDuration(duration: CFTimeInterval, animations: (() -> Void), options: UIViewAnimationOptions? = nil, completion: (() -> Void)? = nil) {
public static func animateWithDuration(duration: CFTimeInterval, animations: (() -> Void), completion: (() -> Void)? = nil) {
CATransaction.begin()
CATransaction.setAnimationDuration(duration)
CATransaction.setCompletionBlock(completion)
......@@ -134,9 +132,9 @@ public struct MaterialAnimation {
/**
:name: animateWithDelay
*/
public static func animateWithDelay(delay d: CFTimeInterval, duration: CFTimeInterval, animations: (() -> Void), options: UIViewAnimationOptions? = nil, completion: (() -> Void)? = nil) {
public static func animateWithDelay(delay d: CFTimeInterval, duration: CFTimeInterval, animations: (() -> Void), completion: (() -> Void)? = nil) {
delay(d) {
animateWithDuration(duration, animations: animations, options: options, completion: completion)
animateWithDuration(duration, animations: animations, completion: completion)
}
}
}
......@@ -632,7 +632,7 @@ public class TextField : UITextField {
/// Layout the clearButton.
private func layoutClearButton() {
if 0 < width && 0 < height {
clearButton.frame = CGRectMake(width - height, 0, height, height)
// clearButton.frame = CGRectMake(width - height, 0, height, height)
}
}
......
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