Commit 5cbae5db by Orkhan Alikhanov

Added errorLabel to ErrorTextField

parent 87ac65d4
...@@ -31,18 +31,65 @@ ...@@ -31,18 +31,65 @@
import UIKit import UIKit
open class ErrorTextField: TextField { open class ErrorTextField: TextField {
/// Controls the visibility of detailLabel
/// The errorLabel UILabel that is displayed.
@IBInspectable
open let errorLabel = UILabel()
/// The errorLabel text value.
@IBInspectable
open var error: String? {
get {
return errorLabel.text
}
set(value) {
errorLabel.text = value
layoutSubviews()
}
}
/// Error text color
@IBInspectable
open var errorColor = Color.red.base {
didSet {
errorLabel.textColor = errorColor
}
}
/// Vertical distance for the errorLabel from the divider.
@IBInspectable @IBInspectable
open var isErrorRevealed = false { open var errorVerticalOffset: CGFloat = 8 {
didSet { didSet {
detailLabel.isHidden = !isErrorRevealed
layoutSubviews() layoutSubviews()
} }
} }
open var isErrorRevealed: Bool {
get {
return !errorLabel.isHidden
}
set {
errorLabel.isHidden = !newValue
detailLabel.isHidden = newValue
}
}
open override func prepare() { open override func prepare() {
super.prepare() super.prepare()
isErrorRevealed = false isErrorRevealed = false
detailColor = Color.red.base prepareErrorLabel()
}
/// Prepares the errorLabel.
func prepareErrorLabel() {
errorLabel.font = RobotoFont.regular(with: 12)
errorLabel.numberOfLines = 0
errorColor = { errorColor }() // call didSet
addSubview(errorLabel)
}
open override func layoutSubviews() {
super.layoutSubviews()
layoutBottomLabel(label: errorLabel, verticalOffset: errorVerticalOffset)
} }
} }
...@@ -268,7 +268,7 @@ open class TextField: UITextField { ...@@ -268,7 +268,7 @@ open class TextField: UITextField {
@IBInspectable @IBInspectable
open var detailVerticalOffset: CGFloat = 8 { open var detailVerticalOffset: CGFloat = 8 {
didSet { didSet {
layoutDetailLabel() layoutSubviews()
} }
} }
...@@ -412,7 +412,7 @@ open class TextField: UITextField { ...@@ -412,7 +412,7 @@ open class TextField: UITextField {
super.layoutSubviews() super.layoutSubviews()
layoutShape() layoutShape()
layoutPlaceholderLabel() layoutPlaceholderLabel()
layoutDetailLabel() layoutBottomLabel(label: detailLabel, verticalOffset: detailVerticalOffset)
layoutButton(button: clearIconButton) layoutButton(button: clearIconButton)
layoutButton(button: visibilityIconButton) layoutButton(button: visibilityIconButton)
layoutDivider() layoutDivider()
...@@ -573,16 +573,7 @@ fileprivate extension TextField { ...@@ -573,16 +573,7 @@ fileprivate extension TextField {
placeholderLabel.frame.origin.y = -placeholderLabel.bounds.height + placeholderVerticalOffset placeholderLabel.frame.origin.y = -placeholderLabel.bounds.height + placeholderVerticalOffset
} }
/// Layout the detailLabel.
func layoutDetailLabel() {
let c = dividerContentEdgeInsets
detailLabel.frame.size.height = detailLabel.sizeThatFits(CGSize(width: bounds.width, height: .greatestFiniteMagnitude)).height
detailLabel.frame.origin.x = c.left
detailLabel.frame.origin.y = bounds.height + detailVerticalOffset
detailLabel.frame.size.width = bounds.width - c.left - c.right
}
/// Layout the a button. /// Layout the a button.
func layoutButton(button: UIButton?) { func layoutButton(button: UIButton?) {
button?.frame = CGRect(x: bounds.width - bounds.height, y: 0, width: bounds.height, height: bounds.height) button?.frame = CGRect(x: bounds.width - bounds.height, y: 0, width: bounds.height, height: bounds.height)
...@@ -600,6 +591,17 @@ fileprivate extension TextField { ...@@ -600,6 +591,17 @@ fileprivate extension TextField {
} }
} }
internal extension TextField {
/// Layout given label at the bottom with the vertical offset provided.
func layoutBottomLabel(label: UILabel, verticalOffset: CGFloat) {
let c = dividerContentEdgeInsets
label.frame.origin.x = c.left
label.frame.origin.y = bounds.height + verticalOffset
label.frame.size.width = bounds.width - c.left - c.right
label.frame.size.height = label.sizeThatFits(CGSize(width: label.bounds.width, height: .greatestFiniteMagnitude)).height
}
}
fileprivate extension TextField { fileprivate extension TextField {
/// Handles the text editing did begin state. /// Handles the text editing did begin state.
@objc @objc
......
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