Commit 541aaec7 by Daniel Dahan

added MTextField for testing

parent 2f724180
......@@ -7,8 +7,6 @@
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 */; };
......@@ -22,7 +20,6 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
961776721CCE75EB0091B4F3 /* Material.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
......@@ -30,7 +27,6 @@
/* 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>"; };
......@@ -44,7 +40,6 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
961776711CCE75EB0091B4F3 /* Material.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......@@ -54,7 +49,6 @@
966F57941C226BAA009185B7 = {
isa = PBXGroup;
children = (
961776701CCE75EB0091B4F3 /* Material.framework */,
966F579F1C226BAA009185B7 /* TextField */,
966F579E1C226BAA009185B7 /* Products */,
);
......
......@@ -94,6 +94,7 @@ class ViewController: UIViewController, TextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
prepareView()
prepareResignResponderButton()
prepareNameField()
prepareEmailField()
}
......@@ -103,11 +104,32 @@ class ViewController: UIViewController, TextFieldDelegate {
view.backgroundColor = MaterialColor.white
}
/// Prepares the resign responder button.
private func prepareResignResponderButton() {
let btn: RaisedButton = RaisedButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(handleResignResponderButton), forControlEvents: .TouchUpInside)
btn.setTitle("Resign", forState: .Normal)
btn.setTitleColor(MaterialColor.blue.base, forState: .Normal)
btn.setTitleColor(MaterialColor.blue.base, forState: .Highlighted)
view.addSubview(btn)
MaterialLayout.alignFromBottomRight(view, child: btn, bottom: 24, right: 24)
MaterialLayout.size(view, child: btn, width: 100, height: 50)
}
/// Handle the resign responder button.
internal func handleResignResponderButton() {
nameField?.resignFirstResponder()
emailField?.resignFirstResponder()
}
/// Prepares the name TextField.
private func prepareNameField() {
nameField = MTextField()
nameField.placeholder = "Email"
nameField.detail = "Enter your email address."
nameField.clearButtonMode = .WhileEditing
// The translatesAutoresizingMaskIntoConstraints property must be set to enable AutoLayout correctly.
nameField.translatesAutoresizingMaskIntoConstraints = false
......@@ -116,15 +138,16 @@ class ViewController: UIViewController, TextFieldDelegate {
// Size the TextField to the maximum width, less 20 pixels on either side
// with a top margin of 100 pixels.
MaterialLayout.alignFromTop(view, child: nameField, top: 100)
MaterialLayout.alignToParentHorizontally(view, child: nameField, left: 20, right: 20)
MaterialLayout.alignToParentHorizontally(view, child: nameField, left: 40, right: 40)
}
/// Prepares the email TextField.
private func prepareEmailField() {
emailField = MTextField(frame: CGRectMake(20, 200, view.bounds.width - 40, 16))
emailField = MTextField(frame: CGRectMake(40, 200, view.bounds.width - 80, 21))
emailField.placeholder = "Email"
emailField.detail = "Error, incorrect email."
emailField.delegate = self
emailField.clearButtonMode = .WhileEditing
view.addSubview(emailField)
}
......@@ -135,30 +158,24 @@ class ViewController: UIViewController, TextFieldDelegate {
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
print("Should Begin")
return true
}
func textFieldDidBeginEditing(textField: UITextField) {
print("Did Begin")
}
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
print("Should End")
return true
}
func textFieldDidEndEditing(textField: UITextField) {
print("Did End")
}
func textFieldShouldClear(textField: UITextField) -> Bool {
print("Should Clear")
return true
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
print("Should Change")
return true
}
}
......@@ -34,6 +34,8 @@ 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.
......@@ -121,7 +123,22 @@ public class MTextField : UITextField {
@IBInspectable public var dividerHeight: CGFloat = 1
/// Divider active state height.
@IBInspectable public var dividerActiveHeight: CGFloat = 2
@IBInspectable public var dividerActiveHeight: CGFloat = 2
/// Sets the divider and tintColor.
@IBInspectable public var dividerColor: UIColor? {
get {
return nil == divider.backgroundColor ? nil : UIColor(CGColor: divider.backgroundColor!)
}
set(value) {
divider.backgroundColor = dividerColor?.CGColor
if let v: UIColor = dividerColor {
tintColor = v
} else {
tintColor = MaterialColor.darkText.dividers
}
}
}
/// The placeholderLabel font value.
@IBInspectable public override var font: UIFont? {
......@@ -141,7 +158,7 @@ public class MTextField : UITextField {
set(value) {
placeholderLabel.text = value
if let v: String = value {
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderTextColor])
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderColor])
}
}
}
......@@ -154,10 +171,10 @@ public class MTextField : UITextField {
@IBInspectable public private(set) lazy var placeholderLabel: UILabel = UILabel(frame: CGRectZero)
/// Placeholder textColor.
@IBInspectable public var placeholderTextColor: UIColor = MaterialColor.darkText.others {
@IBInspectable public var placeholderColor: UIColor = MaterialColor.darkText.others {
didSet {
if let v: String = placeholder {
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderTextColor])
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderColor])
}
}
}
......@@ -174,16 +191,16 @@ public class MTextField : UITextField {
set(value) {
detailLabel.text = value
if let v: String = value {
detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderTextColor])
detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderColor])
}
}
}
/// Detail textColor.
@IBInspectable public var detailTextColor: UIColor = MaterialColor.darkText.others {
@IBInspectable public var detailColor: UIColor = MaterialColor.darkText.others {
didSet {
if let v: String = detail {
detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailTextColor])
detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailColor])
}
}
}
......@@ -215,7 +232,7 @@ public class MTextField : UITextField {
public override func layoutSubviews() {
super.layoutSubviews()
if !editing {
if !editing && !animating {
layoutPlaceholderLabel()
layoutDetailLabel()
}
......@@ -329,7 +346,8 @@ public class MTextField : UITextField {
} else if CGAffineTransformIsIdentity(placeholderLabel.transform) {
placeholderLabel.frame = bounds
placeholderLabel.transform = CGAffineTransformScale(placeholderLabel.transform, 0.75, 0.75)
placeholderLabel.frame.origin = CGPointMake(0, -24)
placeholderLabel.frame.origin.x = 0
placeholderLabel.frame.origin.y = -21
}
}
......@@ -353,25 +371,34 @@ public class MTextField : UITextField {
/// The animation for the placeholder when editing begins.
public func placeholderEditingDidBeginAnimation() {
placeholderLabel.textColor = placeholderTextColor
if CGAffineTransformIsIdentity(placeholderLabel.transform) {
animating = true
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.origin = CGPointMake(0, -24)
if let v: MTextField = self {
v.placeholderLabel.transform = CGAffineTransformMakeScale(0.75, 0.75)
v.placeholderLabel.frame.origin.x = 0
v.placeholderLabel.frame.origin.y = -21
}
})
}) { [weak self] _ in
self?.animating = false
}
}
}
/// The animation for the placeholder when editing ends.
public func placeholderEditingDidEndAnimation() {
UIView.animateWithDuration(0.15, animations: { [weak self] in
if let s: MTextField = self {
s.placeholderLabel.transform = CGAffineTransformIdentity
s.placeholderLabel.frame = s.bounds
if !CGAffineTransformIsIdentity(placeholderLabel.transform) {
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
}
}) { [weak self] _ in
self?.animating = false
}
})
}
}
/// Prepares the divider.
......@@ -383,14 +410,14 @@ public class MTextField : UITextField {
/// Prepares the placeholderLabel.
private func preparePlaceholderLabel() {
placeholderLabel.font = font
placeholderTextColor = MaterialColor.darkText.others
placeholderColor = MaterialColor.darkText.others
addSubview(placeholderLabel)
}
/// Prepares the detailLabel.
private func prepareDetailLabel() {
detailLabel.font = RobotoFont.regularWithSize(12)
detailTextColor = MaterialColor.darkText.others
detailColor = MaterialColor.darkText.others
addSubview(detailLabel)
}
......
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