Skip to content

Commit

Permalink
Merge pull request #177 from kanjanaSi/disabled-color
Browse files Browse the repository at this point in the history
disabled color
  • Loading branch information
k0nserv authored Nov 1, 2017
2 parents 624c43f + 90b5149 commit 109a275
Show file tree
Hide file tree
Showing 9 changed files with 381 additions and 136 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ class MyViewController: UIViewController, UITextFieldDelegate {
}
```

### Disabled state
The textfield also supports displaying a disabled state. When the `isEnabled` property is set on the control, then the control is highlighted with the color set in the `disabledColor` property.
```swift
textField.disabledColor = disabledColor
textField.isEnabled = false
```
### RTL language support

The component automatically detects the language writing direction. When the phone has a RTL language set (e.g. Arabic or Hebrew), then it automatically adjusts to support this style.
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@
"error message" = "error message";

/* */
"Email not valid" = "Email not valid";
"Email not valid" = "Email not valid";

/* Disable text field*/
"Disable text field" = "Disable text field";

/* Enable text field*/
"Enable text field" = "Enable text field";

Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,10 @@ class SettingTextsViewController: UIViewController {
break
}
}

@IBAction func enabledChanged(_ sender: Any) {
if let textField = textField {
textField.isEnabled = !textField.isEnabled
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class CustomizingColorsViewController: UIViewController {
@IBOutlet weak var errorColorControl: UISegmentedControl?

@IBOutlet var addErrorButton: UIButton?

@IBOutlet var enableButton: UIButton?

// MARK: - view lifecycle

override func viewDidLoad() {
Expand Down Expand Up @@ -85,6 +86,29 @@ class CustomizingColorsViewController: UIViewController {
}
}

@IBAction func toggleEnable(_ sender: Any) {
if let textField = textField {
textField.isEnabled = !textField.isEnabled
if textField.isEnabled {
enableButton?.setTitle(
NSLocalizedString(
"Enable text field",
tableName: "SkyFloatingLabelTextField",
comment: "enable button title"
),
for: .normal)
} else {
enableButton?.setTitle(
NSLocalizedString(
"Disable text field",
tableName: "SkyFloatingLabelTextField",
comment: "enable button title"
),
for: .normal)
}
}
}

@IBAction func resignTextField() {
textField?.resignFirstResponder()
}
Expand Down Expand Up @@ -119,6 +143,10 @@ class CustomizingColorsViewController: UIViewController {
colorForSegmentIndex(segmentIndex: segmentedControl.selectedSegmentIndex)
}

@IBAction func disabledColorChanged(_ segmentedControl: UISegmentedControl) {
textField?.disabledColor =
colorForSegmentIndex(segmentIndex: segmentedControl.selectedSegmentIndex)
}
// MARK: helper

func colorForSegmentIndex(segmentIndex: Int) -> UIColor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,107 @@ class SkyFloatingLabelTextFieldTests: XCTestCase { // swiftlint:disable:this typ
// then
XCTAssertEqual(floatingLabelTextField.titleLabel.textColor, self.customColor)
}

func test_whenSettingDisabledColor_withTextFieldBeingEnabled_thenTitleLabelTextColorIsNotChangedToThisColor() {
// given
floatingLabelTextField.isEnabled = true

// when
floatingLabelTextField.disabledColor = self.customColor

// then
XCTAssertNotEqual(floatingLabelTextField.titleLabel.textColor, self.customColor)
}

func test_whenSettingDisabledColor_withTextFieldBeingEnabled_thenPlaceholderColorIsNotChangedToThisColor() {
// given
floatingLabelTextField.isEnabled = true
floatingLabelTextField.placeholder = "test"
var fullRange =
NSRange(location: 0, length: floatingLabelTextField.placeholder!.characters.count)

// when
floatingLabelTextField.disabledColor = self.customColor

// then
#if swift(>=4.0)
XCTAssertNotEqual(
floatingLabelTextField.attributedPlaceholder!.attribute(
NSAttributedStringKey.foregroundColor, at: 0, effectiveRange: &fullRange
) as? UIColor,
customColor
)
#else
XCTAssertNotEqual(
floatingLabelTextField.attributedPlaceholder!.attribute(
NSForegroundColorAttributeName, at: 0, effectiveRange: &fullRange
) as? UIColor,
customColor
)
#endif
}

func test_whenSettingDisabledColor_withTextFieldBeingEnabled_thenLineViewBackgroundColorIsNotChangedToThisColor() {
// given
floatingLabelTextField.isEnabled = true

// when
floatingLabelTextField.disabledColor = self.customColor

// then
XCTAssertNotEqual(floatingLabelTextField.lineView.backgroundColor, self.customColor)
}

func test_whenSettingDisabledColor_withTextFieldBeingDisabled_thenTitleLabelTextColorIsChangedToThisColor() {
// given
floatingLabelTextField.isEnabled = false

// when
floatingLabelTextField.disabledColor = self.customColor

// then
XCTAssertEqual(floatingLabelTextField.titleLabel.textColor, self.customColor)
}

func test_whenSettingDisabledColor_withTextFieldBeingDisabled_thenPlaceholderColorIsChangedToThisColor() {
// given
floatingLabelTextField.isEnabled = false
floatingLabelTextField.placeholder = "test"
var fullRange =
NSRange(location: 0, length: floatingLabelTextField.placeholder!.characters.count)

// when
floatingLabelTextField.disabledColor = self.customColor

// then
#if swift(>=4.0)
XCTAssertEqual(
floatingLabelTextField.attributedPlaceholder!.attribute(
NSAttributedStringKey.foregroundColor, at: 0, effectiveRange: &fullRange
) as? UIColor,
customColor
)
#else
XCTAssertEqual(
floatingLabelTextField.attributedPlaceholder!.attribute(
NSForegroundColorAttributeName, at: 0, effectiveRange: &fullRange
) as? UIColor,
customColor
)
#endif
}

func test_whenSettingDisabledColor_withTextFieldBeingDisabled_thenLineViewBackgroundColorIsChangedToThisColor() {
// given
floatingLabelTextField.isEnabled = false

// when
floatingLabelTextField.disabledColor = self.customColor

// then
XCTAssertEqual(floatingLabelTextField.lineView.backgroundColor, self.customColor)
}

// MARK: - fonts

func test_whenSettingPlaceholderFont_thenAttributedPlaceholderTextIsSet_withFont() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ class SkyFloatingLabelTextFieldWithIconTests: XCTestCase {
// then
XCTAssertEqual(floatingLabelTextFieldWithIcon.iconLabel.textColor, customColor)
}


func test_whenSettingDisabledColor_withTextFieldBeingDisabled_thenColorAppliedToIconLabel() {
// when
floatingLabelTextFieldWithIcon.disabledColor = customColor
floatingLabelTextFieldWithIcon.isEnabled = false

// then
XCTAssertEqual(floatingLabelTextFieldWithIcon.iconLabel.textColor, customColor)
}

func test_whenSettingSelectedIconColor_withTextFieldBeingSelected_thenColorAppliedToIconLabel() {
// when
floatingLabelTextFieldWithIcon.selectedIconColor = customColor
Expand Down
58 changes: 41 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,14 @@ 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 {
updateControl()
updatePlaceholder()
}
}

/// 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 +369,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 +402,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 +426,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
4 changes: 3 additions & 1 deletion Sources/SkyFloatingLabelTextFieldWithIcon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ open class SkyFloatingLabelTextFieldWithIcon: SkyFloatingLabelTextField {
}

fileprivate func updateIconLabelColor() {
if self.hasErrorMessage {
if !isEnabled {
iconLabel?.textColor = disabledColor
} else if hasErrorMessage {
iconLabel?.textColor = errorColor
} else {
iconLabel?.textColor = editingOrSelected ? selectedIconColor : iconColor
Expand Down

0 comments on commit 109a275

Please sign in to comment.