Skip to content

Commit

Permalink
disable color
Browse files Browse the repository at this point in the history
  • Loading branch information
miang committed Oct 26, 2017
1 parent 624c43f commit 76602eb
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions Sources/SkyFloatingLabelTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,23 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
}

fileprivate func updatePlaceholder() {
if let placeholder = placeholder, let font = placeholderFont ?? font {
#if swift(>=4.0)
attributedPlaceholder = NSAttributedString(
string: placeholder,
attributes: [
NSAttributedStringKey.foregroundColor: placeholderColor, NSAttributedStringKey.font: font
]
)
#else
attributedPlaceholder = NSAttributedString(
string: placeholder,
attributes: [NSForegroundColorAttributeName: placeholderColor, NSFontAttributeName: font]
)
#endif
guard let placeholder = placeholder, let font = placeholderFont ?? font else {
return
}
let color = isEnabled ? placeholderColor : disabledColor
#if swift(>=4.0)
attributedPlaceholder = NSAttributedString(
string: placeholder,
attributes: [
NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: font
]
)
#else
attributedPlaceholder = NSAttributedString(
string: placeholder,
attributes: [NSForegroundColorAttributeName: color, NSFontAttributeName: font]
)
#endif
}

/// A UIFont value that determines the text font of the title label
Expand Down Expand Up @@ -118,6 +120,13 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
updateColors()
}
}

/// A UIColor value that determines the color used for the title label and line when text field is disabled
@IBInspectable dynamic open var disabledColor: UIColor = UIColor(white: 0.88, alpha: 1.0) {
didSet {
updateColors()
}
}

/// A UIColor value that determines the text color of the title label when editing
@IBInspectable dynamic open var selectedTitleColor: UIColor = .blue {
Expand Down Expand Up @@ -359,6 +368,14 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
return result
}

/// update colors when is enabled changed
override open var isEnabled: Bool {
didSet {
updateControl()
updatePlaceholder()
}
}

// MARK: - View updates

fileprivate func updateControl(_ animated: Bool = false) {
Expand All @@ -384,15 +401,19 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
}

fileprivate func updateLineColor() {
if hasErrorMessage {
if !isEnabled {
lineView.backgroundColor = disabledColor
} else if hasErrorMessage {
lineView.backgroundColor = errorColor
} else {
lineView.backgroundColor = editingOrSelected ? selectedLineColor : lineColor
}
}

fileprivate func updateTitleColor() {
if hasErrorMessage {
if !isEnabled {
titleLabel.textColor = disabledColor
} else if hasErrorMessage {
titleLabel.textColor = errorColor
} else {
if editingOrSelected || isHighlighted {
Expand All @@ -404,7 +425,9 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
}

fileprivate func updateTextColor() {
if hasErrorMessage {
if !isEnabled {
super.textColor = disabledColor
} else if hasErrorMessage {
super.textColor = errorColor
} else {
super.textColor = cachedTextColor
Expand Down

0 comments on commit 76602eb

Please sign in to comment.