Skip to content

Commit

Permalink
Merge pull request #208 from Skyscanner/rebuy-de-master
Browse files Browse the repository at this point in the history
Add custom error colors for line, title, text
  • Loading branch information
k0nserv authored May 28, 2018
2 parents edb94b1 + b6c4811 commit 31d9774
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Master
------

* Marked `isLTRLanguage` with `@objc` so it can be set from Objective-C code [#200](https://github.com/Skyscanner/SkyFloatingLabelTextField/pull/200).
* Marked `isLTRLanguage` with `@objc` so it can be set from Objective-C code [#200](https://github.com/Skyscanner/SkyFloatingLabelTextField/pull/200).
* Added support for different colors for line, title, text when error is set.

v.3.4.1
-------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ class SkyFloatingLabelTextFieldTests: XCTestCase { // swiftlint:disable:this typ
XCTAssertEqual(floatingLabelTextField.titleLabel.textColor, self.customColor)
}

func test_whenSettingTitleErrorColor_withErrorMessageBeingSet_thenTitleLabelTextColorIsChangedToThisColor() {
// given
floatingLabelTextField.errorMessage = "test"

// when
floatingLabelTextField.titleErrorColor = self.customColor

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

func test_whenSettingErrorColor_withErrorMessageBeingEmpty_thenTitleLabelTextColorIsNotChangedToThisColor() {
// given
floatingLabelTextField.errorMessage = ""
Expand All @@ -114,6 +125,17 @@ class SkyFloatingLabelTextFieldTests: XCTestCase { // swiftlint:disable:this typ
XCTAssertNotEqual(floatingLabelTextField.titleLabel.textColor, self.customColor)
}

func test_whenSettingTitleErrorColor_withErrorMessageBeingEmpty_thenTitleLabelTextColorIsNotChangedToThisColor() {
// given
floatingLabelTextField.errorMessage = ""

// when
floatingLabelTextField.titleErrorColor = self.customColor

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

func test_whenSettingErrorColor_withErrorMessageBeingNil_thenTitleLabelTextColorIsNotChangedToThisColor() {
// given
floatingLabelTextField.errorMessage = nil
Expand All @@ -125,6 +147,17 @@ class SkyFloatingLabelTextFieldTests: XCTestCase { // swiftlint:disable:this typ
XCTAssertNotEqual(floatingLabelTextField.titleLabel.textColor, self.customColor)
}

func test_whenSettingTitleErrorColor_withErrorMessageBeingNil_thenTitleLabelTextColorIsNotChangedToThisColor() {
// given
floatingLabelTextField.errorMessage = nil

// when
floatingLabelTextField.titleErrorColor = self.customColor

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

func test_whenSettingErrorColor_withErrorMessageBeingSet_thenLineViewBackgroundColorIsChangedToThisColor() {
// given
floatingLabelTextField.errorMessage = "test"
Expand All @@ -136,6 +169,17 @@ class SkyFloatingLabelTextFieldTests: XCTestCase { // swiftlint:disable:this typ
XCTAssertEqual(floatingLabelTextField.lineView.backgroundColor, self.customColor)
}

func test_whenSettingLineErrorColor_withErrorMessageBeingSet_thenLineViewBackgroundColorIsChangedToThisColor() {
// given
floatingLabelTextField.errorMessage = "test"

// when
floatingLabelTextField.lineErrorColor = self.customColor

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

func test_whenSettingSelectedTitleColor_withTextfieldBeingSelected_thenTitleLabelTextColorIsChangedToThisColor() {
// given
floatingLabelTextField.isSelected = true
Expand Down
27 changes: 24 additions & 3 deletions Sources/SkyFloatingLabelTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,27 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
}
}

/// A UIColor value that determines the color used for the line when error message is not `nil`
@IBInspectable dynamic open var lineErrorColor: UIColor? {
didSet {
updateColors()
}
}

/// A UIColor value that determines the color used for the text when error message is not `nil`
@IBInspectable dynamic open var textErrorColor: UIColor? {
didSet {
updateColors()
}
}

/// A UIColor value that determines the color used for the title label when error message is not `nil`
@IBInspectable dynamic open var titleErrorColor: UIColor? {
didSet {
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 {
Expand Down Expand Up @@ -409,7 +430,7 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
if !isEnabled {
lineView.backgroundColor = disabledColor
} else if hasErrorMessage {
lineView.backgroundColor = errorColor
lineView.backgroundColor = lineErrorColor ?? errorColor
} else {
lineView.backgroundColor = editingOrSelected ? selectedLineColor : lineColor
}
Expand All @@ -419,7 +440,7 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
if !isEnabled {
titleLabel.textColor = disabledColor
} else if hasErrorMessage {
titleLabel.textColor = errorColor
titleLabel.textColor = titleErrorColor ?? errorColor
} else {
if editingOrSelected || isHighlighted {
titleLabel.textColor = selectedTitleColor
Expand All @@ -433,7 +454,7 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
if !isEnabled {
super.textColor = disabledColor
} else if hasErrorMessage {
super.textColor = errorColor
super.textColor = textErrorColor ?? errorColor
} else {
super.textColor = cachedTextColor
}
Expand Down

0 comments on commit 31d9774

Please sign in to comment.