Commit 233c49bf by Daniel Dahan

updated TextField for fix 517 and added new API

parent eb94bb7e
...@@ -262,6 +262,7 @@ ...@@ -262,6 +262,7 @@
isa = XCBuildConfiguration; isa = XCBuildConfiguration;
buildSettings = { buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = TextField/Info.plist; INFOPLIST_FILE = TextField/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = io.cosmicmind.TextField; PRODUCT_BUNDLE_IDENTIFIER = io.cosmicmind.TextField;
...@@ -274,6 +275,7 @@ ...@@ -274,6 +275,7 @@
isa = XCBuildConfiguration; isa = XCBuildConfiguration;
buildSettings = { buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = TextField/Info.plist; INFOPLIST_FILE = TextField/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = io.cosmicmind.TextField; PRODUCT_BUNDLE_IDENTIFIER = io.cosmicmind.TextField;
......
...@@ -54,20 +54,19 @@ class ViewController: UIViewController { ...@@ -54,20 +54,19 @@ class ViewController: UIViewController {
/// Prepares the resign responder button. /// Prepares the resign responder button.
private func prepareResignResponderButton() { private func prepareResignResponderButton() {
let btn = RaisedButton(title: "Resign", titleColor: Color.blue.base) let btn = RaisedButton(title: "Resign", titleColor: Color.blue.base)
btn.addTarget(self, action: #selector(handleResignResponderButton), for: .touchUpInside) btn.addTarget(self, action: #selector(handleResignResponderButton(button:)), for: .touchUpInside)
view.layout(btn).width(100).height(50).bottom(24).right(24) view.layout(btn).width(100).height(50).bottom(24).right(24)
} }
/// Handle the resign responder button. /// Handle the resign responder button.
internal func handleResignResponderButton() { @objc
internal func handleResignResponderButton(button: UIButton) {
nameField?.resignFirstResponder() nameField?.resignFirstResponder()
emailField?.resignFirstResponder() emailField?.resignFirstResponder()
passwordField?.resignFirstResponder() passwordField?.resignFirstResponder()
} }
/// Prepares the name TextField.
private func prepareNameField() { private func prepareNameField() {
nameField = TextField() nameField = TextField()
nameField.text = "Daniel Dahan" nameField.text = "Daniel Dahan"
...@@ -81,7 +80,6 @@ class ViewController: UIViewController { ...@@ -81,7 +80,6 @@ class ViewController: UIViewController {
view.layout(nameField).top(40).horizontally(left: 40, right: 40) view.layout(nameField).top(40).horizontally(left: 40, right: 40)
} }
/// Prepares the email TextField.
private func prepareEmailField() { private func prepareEmailField() {
emailField = ErrorTextField(frame: CGRect(x: 40, y: 120, width: view.width - 80, height: 32)) emailField = ErrorTextField(frame: CGRect(x: 40, y: 120, width: view.width - 80, height: 32))
emailField.placeholder = "Email" emailField.placeholder = "Email"
...@@ -89,14 +87,13 @@ class ViewController: UIViewController { ...@@ -89,14 +87,13 @@ class ViewController: UIViewController {
emailField.isClearIconButtonEnabled = true emailField.isClearIconButtonEnabled = true
emailField.delegate = self emailField.delegate = self
emailField.placeholderColor = Color.amber.darken4 emailField.placeholderNormalColor = Color.amber.darken4
emailField.placeholderActiveColor = Color.pink.base emailField.placeholderActiveColor = Color.pink.base
emailField.dividerColor = Color.cyan.base emailField.dividerNormalColor = Color.cyan.base
view.addSubview(emailField) view.addSubview(emailField)
} }
/// Prepares the password TextField.
private func preparePasswordField() { private func preparePasswordField() {
passwordField = TextField() passwordField = TextField()
passwordField.placeholder = "Password" passwordField.placeholder = "Password"
...@@ -104,7 +101,7 @@ class ViewController: UIViewController { ...@@ -104,7 +101,7 @@ class ViewController: UIViewController {
passwordField.clearButtonMode = .whileEditing passwordField.clearButtonMode = .whileEditing
passwordField.isVisibilityIconButtonEnabled = true passwordField.isVisibilityIconButtonEnabled = true
// Setting the visibilityFlatButton color. // Setting the visibilityIconButton color.
passwordField.visibilityIconButton?.tintColor = Color.green.base.withAlphaComponent(passwordField.isSecureTextEntry ? 0.38 : 0.54) passwordField.visibilityIconButton?.tintColor = Color.green.base.withAlphaComponent(passwordField.isSecureTextEntry ? 0.38 : 0.54)
// Size the TextField to the maximum width, less 40 pixels on either side // Size the TextField to the maximum width, less 40 pixels on either side
......
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
import UIKit import UIKit
private var TextFieldContext: UInt8 = 0
public protocol TextFieldDelegate: UITextFieldDelegate {} public protocol TextFieldDelegate: UITextFieldDelegate {}
open class TextField: UITextField { open class TextField: UITextField {
...@@ -41,45 +43,35 @@ open class TextField: UITextField { ...@@ -41,45 +43,35 @@ open class TextField: UITextField {
/// A Boolean that indicates if the TextField is in an animating state. /// A Boolean that indicates if the TextField is in an animating state.
open internal(set) var isAnimating = false open internal(set) var isAnimating = false
/// A property that accesses the backing layer's backgroundColor. /// Divider normal height.
@IBInspectable @IBInspectable
open override var backgroundColor: UIColor? { open var dividerNormalHeight: CGFloat = 1
didSet {
layer.backgroundColor = backgroundColor?.cgColor
} /// Divider active height.
}
/// Divider active state height.
@IBInspectable @IBInspectable
open var dividerActiveHeight: CGFloat = 2 open var dividerActiveHeight: CGFloat = 2
/// Sets the divider. /// Divider normal color.
@IBInspectable @IBInspectable
open override var dividerColor: UIColor? { open var dividerNormalColor = Color.darkText.dividers {
get { didSet {
return super.dividerColor
}
set(value) {
guard !isEditing else { guard !isEditing else {
return return
} }
super.dividerColor = value dividerColor = dividerNormalColor
} }
} }
/// Sets the divider. /// Divider active color.
@IBInspectable @IBInspectable
open var dividerActiveColor: UIColor? { open var dividerActiveColor = Color.blue.base {
didSet { didSet {
guard isEditing else { guard isEditing else {
return return
} }
guard let v = dividerActiveColor else { dividerColor = dividerActiveColor
return
}
dividerColor = v
} }
} }
...@@ -110,7 +102,7 @@ open class TextField: UITextField { ...@@ -110,7 +102,7 @@ open class TextField: UITextField {
set(value) { set(value) {
placeholderLabel.text = value placeholderLabel.text = value
if let v: String = value { if let v: String = value {
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderColor]) placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderNormalColor])
} }
} }
} }
...@@ -119,13 +111,13 @@ open class TextField: UITextField { ...@@ -119,13 +111,13 @@ open class TextField: UITextField {
@IBInspectable @IBInspectable
open private(set) var placeholderLabel: UILabel! open private(set) var placeholderLabel: UILabel!
/// Placeholder textColor. /// Placeholder normal textColor.
@IBInspectable @IBInspectable
open var placeholderColor = Color.darkText.others { open var placeholderNormalColor = Color.darkText.others {
didSet { didSet {
if !isEditing { if !isEditing {
if let v: String = placeholder { if let v: String = placeholder {
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderColor]) placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderNormalColor])
} }
} }
} }
...@@ -145,13 +137,13 @@ open class TextField: UITextField { ...@@ -145,13 +137,13 @@ open class TextField: UITextField {
} }
/// This property adds a padding to placeholder y position animation /// This property adds a padding to placeholder y position animation
open var placeholderVerticalOffset: CGFloat = 0 @IBInspectable
open var placeholderVerticalOffset: CGFloat = 0
/// The detailLabel UILabel that is displayed. /// The detailLabel UILabel that is displayed.
@IBInspectable @IBInspectable
open private(set) lazy var detailLabel = UILabel(frame: .zero) open private(set) lazy var detailLabel = UILabel(frame: .zero)
/// The detailLabel text value. /// The detailLabel text value.
@IBInspectable @IBInspectable
open var detail: String? { open var detail: String? {
...@@ -160,10 +152,6 @@ open class TextField: UITextField { ...@@ -160,10 +152,6 @@ open class TextField: UITextField {
} }
set(value) { set(value) {
detailLabel.text = value detailLabel.text = value
if let v: String = value {
detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailColor])
}
layoutDetailLabel()
} }
} }
...@@ -171,7 +159,7 @@ open class TextField: UITextField { ...@@ -171,7 +159,7 @@ open class TextField: UITextField {
@IBInspectable @IBInspectable
open var detailColor = Color.darkText.others { open var detailColor = Color.darkText.others {
didSet { didSet {
if let v: String = detailLabel.text { if let v = detailLabel.text {
detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailColor]) detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailColor])
} }
} }
...@@ -206,7 +194,7 @@ open class TextField: UITextField { ...@@ -206,7 +194,7 @@ open class TextField: UITextField {
set(value) { set(value) {
if value { if value {
if nil == clearIconButton { if nil == clearIconButton {
clearIconButton = IconButton(image: Icon.cm.clear, tintColor: placeholderColor) clearIconButton = IconButton(image: Icon.cm.clear, tintColor: placeholderNormalColor)
clearIconButton!.contentEdgeInsets = .zero clearIconButton!.contentEdgeInsets = .zero
clearIconButton!.pulseAnimation = .center clearIconButton!.pulseAnimation = .center
clearButtonMode = .never clearButtonMode = .never
...@@ -241,7 +229,7 @@ open class TextField: UITextField { ...@@ -241,7 +229,7 @@ open class TextField: UITextField {
set(value) { set(value) {
if value { if value {
if nil == visibilityIconButton { if nil == visibilityIconButton {
visibilityIconButton = IconButton(image: Icon.visibility, tintColor: placeholderColor.withAlphaComponent(isSecureTextEntry ? 0.38 : 0.54)) visibilityIconButton = IconButton(image: Icon.visibility, tintColor: placeholderNormalColor.withAlphaComponent(isSecureTextEntry ? 0.38 : 0.54))
visibilityIconButton!.contentEdgeInsets = .zero visibilityIconButton!.contentEdgeInsets = .zero
visibilityIconButton!.pulseAnimation = .center visibilityIconButton!.pulseAnimation = .center
isSecureTextEntry = true isSecureTextEntry = true
...@@ -285,6 +273,19 @@ open class TextField: UITextField { ...@@ -285,6 +273,19 @@ open class TextField: UITextField {
return super.becomeFirstResponder() return super.becomeFirstResponder()
} }
open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard "detailLabel.text" == keyPath else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}
updateDetailLabelAttributedText()
layoutDetailLabel()
}
deinit {
removeObserver(self, forKeyPath: "titleLabel.text")
}
/** /**
An initializer that initializes the object with a NSCoder object. An initializer that initializes the object with a NSCoder object.
- Parameter aDecoder: A NSCoder instance. - Parameter aDecoder: A NSCoder instance.
...@@ -407,7 +408,7 @@ open class TextField: UITextField { ...@@ -407,7 +408,7 @@ open class TextField: UITextField {
default:break default:break
} }
placeholderLabel.y = -placeholderLabel.height + placeholderVerticalOffset placeholderLabel.y = -placeholderLabel.height + placeholderVerticalOffset
placeholderLabel.textColor = placeholderColor placeholderLabel.textColor = placeholderNormalColor
} else { } else {
switch textAlignment { switch textAlignment {
case .left, .natural: case .left, .natural:
...@@ -457,8 +458,8 @@ open class TextField: UITextField { ...@@ -457,8 +458,8 @@ open class TextField: UITextField {
/// The animation for the divider when editing ends. /// The animation for the divider when editing ends.
open func dividerEditingDidEndAnimation() { open func dividerEditingDidEndAnimation() {
// divider.frame.size.height = dividerHeight dividerHeight = dividerNormalHeight
// divider.color = dividerColor dividerColor = dividerNormalColor
} }
/// The animation for the placeholder when editing begins. /// The animation for the placeholder when editing begins.
...@@ -495,13 +496,13 @@ open class TextField: UITextField { ...@@ -495,13 +496,13 @@ open class TextField: UITextField {
s.placeholderLabel.transform = CGAffineTransform.identity s.placeholderLabel.transform = CGAffineTransform.identity
s.placeholderLabel.x = 0 s.placeholderLabel.x = 0
s.placeholderLabel.y = 0 s.placeholderLabel.y = 0
s.placeholderLabel.textColor = s.placeholderColor s.placeholderLabel.textColor = s.placeholderNormalColor
} }
}) { [weak self] _ in }) { [weak self] _ in
self?.isAnimating = false self?.isAnimating = false
} }
} else if !isEditing { } else if !isEditing {
placeholderLabel.textColor = placeholderColor placeholderLabel.textColor = placeholderNormalColor
} }
} }
...@@ -513,7 +514,7 @@ open class TextField: UITextField { ...@@ -513,7 +514,7 @@ open class TextField: UITextField {
/// Prepares the placeholderLabel. /// Prepares the placeholderLabel.
private func preparePlaceholderLabel() { private func preparePlaceholderLabel() {
placeholderLabel = UILabel(frame: .zero) placeholderLabel = UILabel(frame: .zero)
placeholderColor = Color.darkText.others placeholderNormalColor = Color.darkText.others
font = RobotoFont.regular(with: 16) font = RobotoFont.regular(with: 16)
addSubview(placeholderLabel) addSubview(placeholderLabel)
} }
...@@ -524,6 +525,7 @@ open class TextField: UITextField { ...@@ -524,6 +525,7 @@ open class TextField: UITextField {
detailLabel.numberOfLines = 0 detailLabel.numberOfLines = 0
detailColor = Color.darkText.others detailColor = Color.darkText.others
addSubview(detailLabel) addSubview(detailLabel)
addObserver(self, forKeyPath: "detailLabel.text", options: [], context: &TextFieldContext)
} }
/// Prepares the target handlers. /// Prepares the target handlers.
...@@ -536,4 +538,11 @@ open class TextField: UITextField { ...@@ -536,4 +538,11 @@ open class TextField: UITextField {
private func prepareTextAlignment() { private func prepareTextAlignment() {
textAlignment = .rightToLeft == UIApplication.shared.userInterfaceLayoutDirection ? .right : .left textAlignment = .rightToLeft == UIApplication.shared.userInterfaceLayoutDirection ? .right : .left
} }
/// Updates the detailLabel attributedText.
private func updateDetailLabelAttributedText() {
if let v = detail {
detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailColor])
}
}
} }
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