Skip to content
This repository was archived by the owner on Jan 16, 2026. It is now read-only.

Commit 31d9774

Browse files
authored
Merge pull request #208 from Skyscanner/rebuy-de-master
Add custom error colors for line, title, text
2 parents edb94b1 + b6c4811 commit 31d9774

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
Master
22
------
33

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

67
v.3.4.1
78
-------

SkyFloatingLabelTextField/SkyFloatingLabelTextFieldTests/SkyFloatingLabelTextFieldTests.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ class SkyFloatingLabelTextFieldTests: XCTestCase { // swiftlint:disable:this typ
103103
XCTAssertEqual(floatingLabelTextField.titleLabel.textColor, self.customColor)
104104
}
105105

106+
func test_whenSettingTitleErrorColor_withErrorMessageBeingSet_thenTitleLabelTextColorIsChangedToThisColor() {
107+
// given
108+
floatingLabelTextField.errorMessage = "test"
109+
110+
// when
111+
floatingLabelTextField.titleErrorColor = self.customColor
112+
113+
// then
114+
XCTAssertEqual(floatingLabelTextField.titleLabel.textColor, self.customColor)
115+
}
116+
106117
func test_whenSettingErrorColor_withErrorMessageBeingEmpty_thenTitleLabelTextColorIsNotChangedToThisColor() {
107118
// given
108119
floatingLabelTextField.errorMessage = ""
@@ -114,6 +125,17 @@ class SkyFloatingLabelTextFieldTests: XCTestCase { // swiftlint:disable:this typ
114125
XCTAssertNotEqual(floatingLabelTextField.titleLabel.textColor, self.customColor)
115126
}
116127

128+
func test_whenSettingTitleErrorColor_withErrorMessageBeingEmpty_thenTitleLabelTextColorIsNotChangedToThisColor() {
129+
// given
130+
floatingLabelTextField.errorMessage = ""
131+
132+
// when
133+
floatingLabelTextField.titleErrorColor = self.customColor
134+
135+
// then
136+
XCTAssertNotEqual(floatingLabelTextField.titleLabel.textColor, self.customColor)
137+
}
138+
117139
func test_whenSettingErrorColor_withErrorMessageBeingNil_thenTitleLabelTextColorIsNotChangedToThisColor() {
118140
// given
119141
floatingLabelTextField.errorMessage = nil
@@ -125,6 +147,17 @@ class SkyFloatingLabelTextFieldTests: XCTestCase { // swiftlint:disable:this typ
125147
XCTAssertNotEqual(floatingLabelTextField.titleLabel.textColor, self.customColor)
126148
}
127149

150+
func test_whenSettingTitleErrorColor_withErrorMessageBeingNil_thenTitleLabelTextColorIsNotChangedToThisColor() {
151+
// given
152+
floatingLabelTextField.errorMessage = nil
153+
154+
// when
155+
floatingLabelTextField.titleErrorColor = self.customColor
156+
157+
// then
158+
XCTAssertNotEqual(floatingLabelTextField.titleLabel.textColor, self.customColor)
159+
}
160+
128161
func test_whenSettingErrorColor_withErrorMessageBeingSet_thenLineViewBackgroundColorIsChangedToThisColor() {
129162
// given
130163
floatingLabelTextField.errorMessage = "test"
@@ -136,6 +169,17 @@ class SkyFloatingLabelTextFieldTests: XCTestCase { // swiftlint:disable:this typ
136169
XCTAssertEqual(floatingLabelTextField.lineView.backgroundColor, self.customColor)
137170
}
138171

172+
func test_whenSettingLineErrorColor_withErrorMessageBeingSet_thenLineViewBackgroundColorIsChangedToThisColor() {
173+
// given
174+
floatingLabelTextField.errorMessage = "test"
175+
176+
// when
177+
floatingLabelTextField.lineErrorColor = self.customColor
178+
179+
// then
180+
XCTAssertEqual(floatingLabelTextField.lineView.backgroundColor, self.customColor)
181+
}
182+
139183
func test_whenSettingSelectedTitleColor_withTextfieldBeingSelected_thenTitleLabelTextColorIsChangedToThisColor() {
140184
// given
141185
floatingLabelTextField.isSelected = true

Sources/SkyFloatingLabelTextField.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,27 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
121121
}
122122
}
123123

124+
/// A UIColor value that determines the color used for the line when error message is not `nil`
125+
@IBInspectable dynamic open var lineErrorColor: UIColor? {
126+
didSet {
127+
updateColors()
128+
}
129+
}
130+
131+
/// A UIColor value that determines the color used for the text when error message is not `nil`
132+
@IBInspectable dynamic open var textErrorColor: UIColor? {
133+
didSet {
134+
updateColors()
135+
}
136+
}
137+
138+
/// A UIColor value that determines the color used for the title label when error message is not `nil`
139+
@IBInspectable dynamic open var titleErrorColor: UIColor? {
140+
didSet {
141+
updateColors()
142+
}
143+
}
144+
124145
/// A UIColor value that determines the color used for the title label and line when text field is disabled
125146
@IBInspectable dynamic open var disabledColor: UIColor = UIColor(white: 0.88, alpha: 1.0) {
126147
didSet {
@@ -409,7 +430,7 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
409430
if !isEnabled {
410431
lineView.backgroundColor = disabledColor
411432
} else if hasErrorMessage {
412-
lineView.backgroundColor = errorColor
433+
lineView.backgroundColor = lineErrorColor ?? errorColor
413434
} else {
414435
lineView.backgroundColor = editingOrSelected ? selectedLineColor : lineColor
415436
}
@@ -419,7 +440,7 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
419440
if !isEnabled {
420441
titleLabel.textColor = disabledColor
421442
} else if hasErrorMessage {
422-
titleLabel.textColor = errorColor
443+
titleLabel.textColor = titleErrorColor ?? errorColor
423444
} else {
424445
if editingOrSelected || isHighlighted {
425446
titleLabel.textColor = selectedTitleColor
@@ -433,7 +454,7 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
433454
if !isEnabled {
434455
super.textColor = disabledColor
435456
} else if hasErrorMessage {
436-
super.textColor = errorColor
457+
super.textColor = textErrorColor ?? errorColor
437458
} else {
438459
super.textColor = cachedTextColor
439460
}

0 commit comments

Comments
 (0)