Commit 2f724180 by Daniel Dahan

added detailTextColor and font wrapper for placeholderLabel within TextField

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