Commit 8d81d569 by Daniel Dahan

TextField added with example

parent 29b6da8a
......@@ -19,25 +19,50 @@
import UIKit
import MaterialKit
class ViewController: UIViewController {
class ViewController: UIViewController, TextFieldDelegate {
private lazy var titleTextField: TextField = TextField()
override func viewDidLoad() {
super.viewDidLoad()
prepareView()
prepareTextField()
prepareTitleTextField()
}
/**
:name: prepareView
*/
private func prepareView() {
view.backgroundColor = MaterialColor.white
}
private func prepareTextField() {
let textField: TextField = TextField(frame: CGRectMake(100, 100, 200, 35))
textField.placeholder = "Title"
textField.textColor = MaterialColor.grey.base
textField.titleLabel = UILabel()
view.addSubview(textField)
/**
:name: prepareTitleTextField
:description: A preparation helper method for titleTextField.
*/
private func prepareTitleTextField() {
titleTextField.frame = CGRectMake(100, 100, 200, 35)
titleTextField.placeholder = "Title"
titleTextField.textColor = MaterialColor.black
titleTextField.titleLabel = UILabel()
titleTextField.titleLabel!.font = RobotoFont.boldWithSize(12)
titleTextField.font = RobotoFont.boldWithSize(24)
titleTextField.delegate = self
titleTextField.titleNormalColor = MaterialColor.grey.lighten1
titleTextField.titleHighlightedColor = MaterialColor.blue.accent3
view.addSubview(titleTextField)
}
/**
:name: textFieldShouldReturn
:description: This is called when the user presses the Return
key on the keyboard.
*/
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == titleTextField {
titleTextField.resignFirstResponder()
}
return false
}
}
Pod::Spec.new do |s|
s.name = 'MK'
s.version = '1.25.3'
s.version = '1.25.4'
s.license = { :type => "AGPL-3.0", :file => "LICENSE" }
s.summary = 'Beautiful Material Design in Swift.'
s.homepage = 'http://materialkit.io'
......
......@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.25.3</string>
<string>1.25.4</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
......
......@@ -56,7 +56,7 @@ public class MaterialLabel : UILabel {
*/
public override var font: UIFont! {
didSet {
textLayer.font = font
textLayer.fontType = font
}
}
......
......@@ -18,8 +18,29 @@
import UIKit
public protocol TextFieldDelegate : UITextFieldDelegate {}
public class TextField : UITextField {
/**
:name: count
*/
private var count: Int?
/**
:name: titleNormalColor
*/
public var titleNormalColor: UIColor? {
didSet {
titleLabel?.textColor = titleNormalColor
}
}
/**
:name: titleHighlightedColor
*/
public var titleHighlightedColor: UIColor?
/**
:name: bottomBorderLayer
*/
public private(set) lazy var bottomBorderLayer: MaterialLayer = MaterialLayer()
......@@ -73,14 +94,73 @@ public class TextField : UITextField {
:name: prepareView
*/
public func prepareView() {
clipsToBounds = false
prepareBottomBorderLayer()
}
/**
:name: textFieldDidBegin
*/
internal func textFieldDidBegin(textField: TextField) {
count = text?.utf16.count
titleLabel?.textColor = titleHighlightedColor
}
/**
:name: textFieldDidChange
*/
internal func textFieldDidChange(textField: TextField) {
if 0 == count && 1 == text?.utf16.count {
if let v: UILabel = titleLabel {
UIView.animateWithDuration(0.25, animations: {
v.hidden = false
v.alpha = 1
v.frame.origin.y = -v.frame.height
})
}
} else if 1 == count && 0 == text?.utf16.count {
if let v: UILabel = titleLabel {
UIView.animateWithDuration(0.25, animations: {
v.alpha = 0
v.frame.origin.y = -v.frame.height / 3
}) { _ in
v.hidden = true
}
}
}
count = text?.utf16.count
}
/**
:name: textFieldDidEnd
*/
internal func textFieldDidEnd(textField: TextField) {
if 0 < count {
if let v: UILabel = titleLabel {
UIView.animateWithDuration(0.25, animations: {
v.hidden = false
v.alpha = 1
v.frame.origin.y = -v.frame.height
})
}
} else if 0 == count {
if let v: UILabel = titleLabel {
UIView.animateWithDuration(0.25, animations: {
v.alpha = 0
v.frame.origin.y = -v.frame.height / 3
}) { _ in
v.hidden = true
}
}
}
titleLabel?.textColor = titleNormalColor
}
/**
:name: prepareBottomBorderLayer
*/
private func prepareBottomBorderLayer() {
bottomBorderLayer.frame = CGRectMake(0, bounds.height - 1, bounds.width, 1)
bottomBorderLayer.frame = CGRectMake(0, bounds.height + 5, bounds.width, 3)
bottomBorderLayer.backgroundColor = MaterialColor.grey.lighten3.CGColor
layer.addSublayer(bottomBorderLayer)
}
......@@ -90,10 +170,17 @@ public class TextField : UITextField {
*/
private func prepareTitleLabel() {
if let v: UILabel = titleLabel {
v.frame = bounds
MaterialAnimation.animationDisabled {
v.hidden = true
v.alpha = 0
}
v.text = placeholder
v.textColor = textColor
let h: CGFloat = v.font.stringSize(v.text!, constrainedToWidth: Double(bounds.width)).height
v.frame = CGRectMake(0, -h, bounds.width, h)
addSubview(v)
addTarget(self, action: "textFieldDidBegin:", forControlEvents: .EditingDidBegin)
addTarget(self, action: "textFieldDidChange:", forControlEvents: .EditingChanged)
addTarget(self, action: "textFieldDidEnd:", 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