Commit a6297afa by Daniel Dahan

minor cleanup to TextField, and a correction when removing KVO on the detailLabel.text keyPath

parent 233c49bf
......@@ -358,6 +358,15 @@
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
96090B031D9D709E00709CA6 /* TextField */ = {
isa = PBXGroup;
children = (
96BCB79C1CB40DC500C806FE /* TextField.swift */,
961F18E71CD93E3E008927C5 /* ErrorTextField.swift */,
);
name = TextField;
sourceTree = "<group>";
};
96230AB61D6A51FD00AF47DC /* Divider */ = {
isa = PBXGroup;
children = (
......@@ -581,7 +590,8 @@
968C99421D36EC9E000074FF /* Switch */,
963FBF011D6696AB008F8512 /* TabBar */,
966ECF2B1CF4C21B00BB0BDF /* Table */,
96BCB80E1CB4110E00C806FE /* Text */,
96090B031D9D709E00709CA6 /* TextField */,
96BCB80E1CB4110E00C806FE /* TextView */,
963FBF001D66964F008F8512 /* Toolbar */,
96BCB8061CB40FD000C806FE /* Type */,
96BCB80C1CB410DD00C806FE /* View */,
......@@ -768,16 +778,14 @@
name = Layer;
sourceTree = "<group>";
};
96BCB80E1CB4110E00C806FE /* Text */ = {
96BCB80E1CB4110E00C806FE /* TextView */ = {
isa = PBXGroup;
children = (
96BCB79B1CB40DC500C806FE /* Text.swift */,
96BCB79C1CB40DC500C806FE /* TextField.swift */,
961F18E71CD93E3E008927C5 /* ErrorTextField.swift */,
96BCB79D1CB40DC500C806FE /* TextStorage.swift */,
96BCB79E1CB40DC500C806FE /* TextView.swift */,
);
name = Text;
name = TextView;
sourceTree = "<group>";
};
96D88BF41C1328D800B91418 /* Sources */ = {
......
......@@ -59,6 +59,7 @@ open class TextField: UITextField {
guard !isEditing else {
return
}
dividerColor = dividerNormalColor
}
}
......@@ -87,9 +88,15 @@ open class TextField: UITextField {
@IBInspectable
open override var text: String? {
didSet {
if true == text?.isEmpty && !isFirstResponder {
placeholderEditingDidEndAnimation()
guard true == text?.isEmpty else {
return
}
guard !isFirstResponder else {
return
}
placeholderEditingDidEndAnimation()
}
}
......@@ -101,9 +108,12 @@ open class TextField: UITextField {
}
set(value) {
placeholderLabel.text = value
if let v: String = value {
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderNormalColor])
guard let v = value else {
return
}
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderNormalColor])
}
}
......@@ -115,11 +125,15 @@ open class TextField: UITextField {
@IBInspectable
open var placeholderNormalColor = Color.darkText.others {
didSet {
if !isEditing {
if let v: String = placeholder {
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderNormalColor])
guard !isEditing else {
return
}
guard let v = placeholder else {
return
}
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderNormalColor])
}
}
......@@ -127,12 +141,17 @@ open class TextField: UITextField {
@IBInspectable
open var placeholderActiveColor = Color.blue.base {
didSet {
if isEditing {
if let v: String = placeholder {
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderActiveColor])
tintColor = placeholderActiveColor
guard isEditing else {
return
}
guard let v = placeholder else {
return
}
tintColor = placeholderActiveColor
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderActiveColor])
}
}
......@@ -159,9 +178,7 @@ open class TextField: UITextField {
@IBInspectable
open var detailColor = Color.darkText.others {
didSet {
if let v = detailLabel.text {
detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailColor])
}
updateDetailLabelAttributedText()
}
}
......@@ -192,8 +209,16 @@ open class TextField: UITextField {
return nil != clearIconButton
}
set(value) {
if value {
if nil == clearIconButton {
guard value else {
clearIconButton?.removeTarget(self, action: #selector(handleClearIconButton), for: .touchUpInside)
clearIconButton = nil
return
}
guard nil == clearIconButton else {
return
}
clearIconButton = IconButton(image: Icon.cm.clear, tintColor: placeholderNormalColor)
clearIconButton!.contentEdgeInsets = .zero
clearIconButton!.pulseAnimation = .center
......@@ -202,11 +227,6 @@ open class TextField: UITextField {
rightView = clearIconButton
isClearIconButtonAutoHandled = isClearIconButtonAutoHandled ? true : false
}
} else {
clearIconButton?.removeTarget(self, action: #selector(handleClearIconButton), for: .touchUpInside)
clearIconButton = nil
}
}
}
/// Enables the automatic handling of the clearIconButton.
......@@ -214,9 +234,12 @@ open class TextField: UITextField {
open var isClearIconButtonAutoHandled = true {
didSet {
clearIconButton?.removeTarget(self, action: #selector(handleClearIconButton), for: .touchUpInside)
if isClearIconButtonAutoHandled {
clearIconButton?.addTarget(self, action: #selector(handleClearIconButton), for: .touchUpInside)
guard isClearIconButtonAutoHandled else {
return
}
clearIconButton?.addTarget(self, action: #selector(handleClearIconButton), for: .touchUpInside)
}
}
......@@ -227,8 +250,16 @@ open class TextField: UITextField {
return nil != visibilityIconButton
}
set(value) {
if value {
if nil == visibilityIconButton {
guard value else {
visibilityIconButton?.removeTarget(self, action: #selector(handleVisibilityIconButton), for: .touchUpInside)
visibilityIconButton = nil
return
}
guard nil == visibilityIconButton else {
return
}
visibilityIconButton = IconButton(image: Icon.visibility, tintColor: placeholderNormalColor.withAlphaComponent(isSecureTextEntry ? 0.38 : 0.54))
visibilityIconButton!.contentEdgeInsets = .zero
visibilityIconButton!.pulseAnimation = .center
......@@ -238,11 +269,6 @@ open class TextField: UITextField {
rightView = visibilityIconButton
isVisibilityIconButtonAutoHandled = isVisibilityIconButtonAutoHandled ? true : false
}
} else {
visibilityIconButton?.removeTarget(self, action: #selector(handleVisibilityIconButton), for: .touchUpInside)
visibilityIconButton = nil
}
}
}
/// Enables the automatic handling of the visibilityIconButton.
......@@ -250,9 +276,12 @@ open class TextField: UITextField {
open var isVisibilityIconButtonAutoHandled: Bool = true {
didSet {
visibilityIconButton?.removeTarget(self, action: #selector(handleVisibilityIconButton), for: .touchUpInside)
if isVisibilityIconButtonAutoHandled {
visibilityIconButton?.addTarget(self, action: #selector(handleVisibilityIconButton), for: .touchUpInside)
guard isVisibilityIconButtonAutoHandled else {
return
}
visibilityIconButton?.addTarget(self, action: #selector(handleVisibilityIconButton), for: .touchUpInside)
}
}
......@@ -278,12 +307,13 @@ open class TextField: UITextField {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}
updateDetailLabelAttributedText()
layoutDetailLabel()
}
deinit {
removeObserver(self, forKeyPath: "titleLabel.text")
removeObserver(self, forKeyPath: "detailLabel.text")
}
/**
......@@ -541,8 +571,10 @@ open class TextField: UITextField {
/// Updates the detailLabel attributedText.
private func updateDetailLabelAttributedText() {
if let v = detail {
detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailColor])
guard let v = detail else {
return
}
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