Commit 2f724180 by Daniel Dahan

added detailTextColor and font wrapper for placeholderLabel within TextField

parent 326addb7
......@@ -89,7 +89,7 @@ import Material
class ViewController: UIViewController, TextFieldDelegate {
private var nameField: MTextField!
private var emailField: TextField!
private var emailField: MTextField!
override func viewDidLoad() {
super.viewDidLoad()
......@@ -106,41 +106,31 @@ class ViewController: UIViewController, TextFieldDelegate {
/// Prepares the name TextField.
private func prepareNameField() {
nameField = MTextField()
nameField.placeholder = "Name yyy yyy "
nameField.detail = "Error, incorrect password. yy ypp ggg"
nameField.text = "Testing Text ppp ppp"
view.addSubview(nameField)
nameField.placeholder = "Email"
nameField.detail = "Enter your email address."
// The translatesAutoresizingMaskIntoConstraints property must be set to enable AutoLayout correctly.
nameField.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(nameField)
// 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)
print(nameField)
}
/// Prepares the email TextField.
private func prepareEmailField() {
let w: CGFloat = 300
let x: CGFloat = (MaterialDevice.width - w) / 2
emailField = TextField(frame: CGRectMake(x, 200, w, 24))
emailField = MTextField(frame: CGRectMake(20, 200, view.bounds.width - 40, 16))
emailField.placeholder = "Email"
emailField.detail = "Error, incorrect email."
emailField.delegate = self
emailField.text = "Hello World"
/*
Used to display the error message, which is displayed when
the user presses the 'return' key.
*/
emailField.detailLabel.text = "Email is incorrect."
emailField.detailLabel.font = RobotoFont.regularWithSize(12)
emailField.detailLabelActiveColor = MaterialColor.red.accent3
// emailField.detailLabelAutoHideEnabled = false // Uncomment this line to have manual hiding.
view.addSubview(emailField)
}
/// Executed when the 'return' key is pressed when using the emailField.
func textFieldShouldReturn(textField: UITextField) -> Bool {
(textField as! TextField).detailLabelHidden = 0 == textField.text?.utf16.count
return true
}
......
......@@ -115,14 +115,34 @@ public class MTextField : UITextField {
}
/// Reference to the divider.
public private(set) var divider: CAShapeLayer!
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
/// 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: placeholderTextColor])
}
}
}
......@@ -131,7 +151,7 @@ public class MTextField : UITextField {
placeholderLabel text value is updated with the placeholder text
value before being displayed.
*/
@IBInspectable public private(set) var placeholderLabel: UILabel!
@IBInspectable public private(set) lazy var placeholderLabel: UILabel = UILabel(frame: CGRectZero)
/// Placeholder textColor.
@IBInspectable public var placeholderTextColor: UIColor = MaterialColor.darkText.others {
......@@ -143,12 +163,28 @@ public class MTextField : UITextField {
}
/// The detailLabel UILabel that is displayed.
@IBInspectable public private(set) var detailLabel: UILabel!
@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: placeholderTextColor])
}
}
}
/// Detail textColor.
@IBInspectable public var detailTextColor: UIColor = MaterialColor.darkText.others {
didSet {
detailLabel.text = detail
if let v: String = detail {
detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailTextColor])
}
}
}
......@@ -180,7 +216,6 @@ public class MTextField : UITextField {
public override func layoutSubviews() {
super.layoutSubviews()
if !editing {
layoutIfNeeded()
layoutPlaceholderLabel()
layoutDetailLabel()
}
......@@ -190,7 +225,6 @@ public class MTextField : UITextField {
super.layoutSublayersOfLayer(layer)
if self.layer == layer {
if !editing {
layoutIfNeeded()
layoutDivider()
}
}
......@@ -285,7 +319,7 @@ public class MTextField : UITextField {
/// Layout the divider.
public func layoutDivider() {
divider.frame = CGRectMake(0, height + 8, width, 1)
divider.frame = CGRectMake(0, height + 8, width, dividerHeight)
}
/// Layout the placeholderLabel.
......@@ -307,13 +341,13 @@ public class MTextField : UITextField {
/// The animation for the divider when editing begins.
public func dividerEditingDidBeginAnimation() {
divider.frame.size.height = 2
divider.frame.size.height = dividerActiveHeight
divider.backgroundColor = MaterialColor.blue.base.CGColor
}
/// The animation for the divider when editing ends.
public func dividerEditingDidEndAnimation() {
divider.frame.size.height = 1
divider.frame.size.height = dividerHeight
divider.backgroundColor = MaterialColor.darkText.dividers.CGColor
}
......@@ -326,9 +360,7 @@ public class MTextField : UITextField {
s.placeholderLabel.transform = CGAffineTransformScale(s.placeholderLabel.transform, 0.75, 0.75)
s.placeholderLabel.frame.origin = CGPointMake(0, -24)
}
}) { [unowned self] _ in
print(self.placeholderLabel.frame)
}
})
}
}
......@@ -344,22 +376,21 @@ public class MTextField : UITextField {
/// 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(frame: CGRectZero)
placeholderLabel.font = font
placeholderTextColor = MaterialColor.darkText.others
addSubview(placeholderLabel)
}
/// Prepares the detailLabel.
private func prepareDetailLabel() {
detailLabel = UILabel(frame: CGRectZero)
detailLabel.font = RobotoFont.regularWithSize(12)
detailTextColor = MaterialColor.darkText.others
addSubview(detailLabel)
}
......
......@@ -42,18 +42,18 @@ public struct MaterialColor {
// dark text
public struct darkText {
public static let primary: UIColor = UIColor.blackColor().colorWithAlphaComponent(0.87)
public static let secondary: UIColor = UIColor.blackColor().colorWithAlphaComponent(0.54)
public static let others: UIColor = UIColor.blackColor().colorWithAlphaComponent(0.38)
public static let dividers: UIColor = UIColor.blackColor().colorWithAlphaComponent(0.12)
public static let primary: UIColor = MaterialColor.black.colorWithAlphaComponent(0.87)
public static let secondary: UIColor = MaterialColor.black.colorWithAlphaComponent(0.54)
public static let others: UIColor = MaterialColor.black.colorWithAlphaComponent(0.38)
public static let dividers: UIColor = MaterialColor.black.colorWithAlphaComponent(0.12)
}
// light text
public struct lightText {
public static let primary: UIColor = UIColor.whiteColor()
public static let secondary: UIColor = UIColor.whiteColor().colorWithAlphaComponent(0.7)
public static let others: UIColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)
public static let dividers: UIColor = UIColor.whiteColor().colorWithAlphaComponent(0.12)
public static let primary: UIColor = MaterialColor.white
public static let secondary: UIColor = MaterialColor.white.colorWithAlphaComponent(0.7)
public static let others: UIColor = MaterialColor.white.colorWithAlphaComponent(0.5)
public static let dividers: UIColor = MaterialColor.white.colorWithAlphaComponent(0.12)
}
// red
......
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