Skip to content

Commit

Permalink
Add custom error colors for line, title, text
Browse files Browse the repository at this point in the history
change podspec

add individual error colors for line, title, text

add unit tests for specific error colors

update log
  • Loading branch information
InbarSletean committed Jan 28, 2018
1 parent acc9ee1 commit 6db0376
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Master
------

v3.4.1
------

* Added support for different colors for line, title, text when error is set.

v3.4.0
------

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 @@ -405,7 +426,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 @@ -415,7 +436,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 @@ -429,7 +450,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 6db0376

Please sign in to comment.