Commit 233c49bf by Daniel Dahan

updated TextField for fix 517 and added new API

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