Skip to content

Commit 5dd01a3

Browse files
authored
fix(ios): searcbar hint color before hint property (#6902)
1 parent 0416f7e commit 5dd01a3

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

tests/app/ui/search-bar/search-bar-tests-native.ios.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { Color } from "tns-core-modules/color";
33
import { getColor } from "../helper";
44

55
export function getNativeHintColor(searchBar: SearchBar): Color {
6-
return (<any>searchBar)._placeholderLabel ? getColor((<any>searchBar)._placeholderLabel.textColor) : undefined;
6+
if ((<any>searchBar)._textField) {
7+
const placeholder = (<any>searchBar)._textField.valueForKey("placeholderLabel");
8+
return getColor(placeholder.textColor);
9+
}
10+
11+
return undefined;
712
}
813

914
export function getNativeTextFieldBackgroundColor(searchBar: SearchBar): Color {

tests/app/ui/search-bar/search-bar-tests.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ export var testSearchBarPropertiesWithCSS = function () {
9292
helper.buildUIAndRunTest(_createSearchBarFunc(), function (views: Array<viewModule.View>) {
9393
var searchBar = <searchBarModule.SearchBar>views[0];
9494

95+
searchBar.text = "";
96+
searchBar.hint = "hint css test";
97+
9598
const expectedHintColor = "#0000FF"; // blue
9699
const expectedTextFieldBackgroundColor = "#FF0000"; // red
97100
const expectedFontSize = 30;
@@ -105,8 +108,6 @@ export var testSearchBarPropertiesWithCSS = function () {
105108
TKUnit.assertAreClose(expectedFontSize, fontSizeActualValue, 0.2, "Font Size - Actual: " + fontSizeActualValue + "; Expected: " + expectedFontSize);
106109
}, { pageCss: `
107110
SearchBar {
108-
text: test;
109-
hint: test;
110111
text-field-hint-color: blue;
111112
text-field-background-color: red;
112113
font-size: 30;

tns-core-modules/ui/search-bar/search-bar.ios.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ export class SearchBar extends SearchBarBase {
7171
nativeViewProtected: UISearchBar;
7272
private _delegate;
7373
private __textField: UITextField;
74-
private __placeholderLabel: UILabel;
7574

7675
createNativeView() {
7776
return UISearchBarImpl.new();
@@ -113,16 +112,6 @@ export class SearchBar extends SearchBarBase {
113112
return this.__textField;
114113
}
115114

116-
get _placeholderLabel(): UILabel {
117-
if (!this.__placeholderLabel) {
118-
if (this._textField) {
119-
this.__placeholderLabel = this._textField.valueForKey("placeholderLabel");
120-
}
121-
}
122-
123-
return this.__placeholderLabel;
124-
}
125-
126115
[isEnabledProperty.setNative](value: boolean) {
127116
const nativeView = this.nativeViewProtected;
128117
if (nativeView instanceof UIControl) {
@@ -189,8 +178,7 @@ export class SearchBar extends SearchBarBase {
189178
return "";
190179
}
191180
[hintProperty.setNative](value: string) {
192-
const text = (value === null || value === undefined) ? "" : value.toString();
193-
this.ios.placeholder = text;
181+
this._updateAttributedPlaceholder();
194182
}
195183

196184
[textFieldBackgroundColorProperty.getDefault](): UIColor {
@@ -210,18 +198,30 @@ export class SearchBar extends SearchBarBase {
210198
}
211199

212200
[textFieldHintColorProperty.getDefault](): UIColor {
213-
const placeholderLabel = this._placeholderLabel;
214-
if (placeholderLabel) {
215-
return placeholderLabel.textColor;
216-
}
217-
218201
return null;
219202
}
220203
[textFieldHintColorProperty.setNative](value: Color | UIColor) {
221-
const color = value instanceof Color ? value.ios : value
222-
const placeholderLabel = this._placeholderLabel;
223-
if (placeholderLabel) {
224-
placeholderLabel.textColor = color;
204+
this._updateAttributedPlaceholder();
205+
}
206+
207+
// Very similar to text-field.ios.ts implementation. Maybe unify APIs and base classes?
208+
_updateAttributedPlaceholder(): void {
209+
let stringValue = this.hint;
210+
if (stringValue === null || stringValue === void 0) {
211+
stringValue = "";
212+
} else {
213+
stringValue = stringValue + "";
214+
}
215+
if (stringValue === "") {
216+
// we do not use empty string since initWithStringAttributes does not return proper value and
217+
// nativeView.attributedPlaceholder will be null
218+
stringValue = " ";
219+
}
220+
const attributes: any = {};
221+
if (this.textFieldHintColor) {
222+
attributes[NSForegroundColorAttributeName] = this.textFieldHintColor.ios;
225223
}
224+
const attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(stringValue, attributes);
225+
this._textField.attributedPlaceholder = attributedPlaceholder;
226226
}
227227
}

0 commit comments

Comments
 (0)