Skip to content

Commit 8101a68

Browse files
author
vakrilov
committed
Fixed(IOS): Setting placeholder color on text filed crashes if no hint is set
1 parent fee1dd9 commit 8101a68

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

tests/app/ui/text-field/text-field-tests.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,4 +575,14 @@ export function test_set_placeholder_color() {
575575
let actualColorHex = textFieldTestsNative.getNativePlaceholderColor(view).hex;
576576
TKUnit.assertEqual(actualColorHex, expectedColorHex);
577577
});
578+
}
579+
580+
export function test_set_placeholder_color_when_hint_is_not_set() {
581+
let view = new textFieldModule.TextField();
582+
let expectedColorHex = "#ffff0000";
583+
helper.buildUIAndRunTest(view, function (views: Array<viewModule.View>) {
584+
view.setInlineStyle("placeholder-color: " + expectedColorHex + ";");
585+
let actualColorHex = textFieldTestsNative.getNativePlaceholderColor(view).hex;
586+
TKUnit.assertEqual(actualColorHex, expectedColorHex);
587+
});
578588
}

tns-core-modules/ui/text-field/text-field.ios.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as style from "ui/styling/style";
77
import {View} from "ui/core/view";
88

99
function onSecurePropertyChanged(data: PropertyChangeData) {
10-
var textField = <TextField>data.object;
10+
const textField = <TextField>data.object;
1111
textField.ios.secureTextEntry = data.newValue;
1212
}
1313

@@ -46,7 +46,7 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
4646

4747
owner.dismissSoftInput();
4848

49-
if (owner.formattedText){
49+
if (owner.formattedText) {
5050
owner.formattedText.createFormattedStringCore();
5151
}
5252
}
@@ -86,8 +86,8 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
8686
}
8787
}
8888
}
89-
90-
if (owner.formattedText){
89+
90+
if (owner.formattedText) {
9191
owner.formattedText._updateCharactersInRangeReplacementString(range.location, range.length, replacementString);
9292
}
9393
}
@@ -156,24 +156,24 @@ export class TextField extends common.TextField {
156156
}
157157

158158
public _onHintPropertyChanged(data: PropertyChangeData) {
159-
var textField = <TextField>data.object;
159+
const textField = <TextField>data.object;
160160
textField.ios.placeholder = data.newValue + "";
161161
}
162-
}
162+
};
163163

164164
export class TextFieldStyler implements style.Styler {
165165
private static setColorProperty(view: View, newValue: any) {
166-
var tf: UITextField = <UITextField>view._nativeView;
166+
const tf: UITextField = <UITextField>view._nativeView;
167167
TextFieldStyler._setTextFieldColor(tf, newValue);
168168
}
169169

170170
private static resetColorProperty(view: View, nativeValue: any) {
171-
var tf: UITextField = <UITextField>view._nativeView;
171+
const tf: UITextField = <UITextField>view._nativeView;
172172
TextFieldStyler._setTextFieldColor(tf, nativeValue);
173173
}
174174

175175
private static _setTextFieldColor(tf: UITextField, newValue: any) {
176-
var color: UIColor = <UIColor>newValue;
176+
const color: UIColor = <UIColor>newValue;
177177
if ((<any>tf).isShowingHint && color) {
178178
tf.textColor = (<UIColor>color).colorWithAlphaComponent(0.22);
179179
}
@@ -184,27 +184,32 @@ export class TextFieldStyler implements style.Styler {
184184
}
185185

186186
private static getNativeColorValue(view: View): any {
187-
var tf: UITextField = <UITextField>view._nativeView;
187+
const tf: UITextField = <UITextField>view._nativeView;
188188
return tf.tintColor;
189189
}
190190

191191
// placeholder-color
192192
private static setPlaceholderColorProperty(textBase: View, newValue: any) {
193-
let ios = <UITextField>textBase._nativeView;
194-
let colorAttibutes = NSMutableDictionary.alloc().init();
195-
colorAttibutes.setValueForKey(newValue, NSForegroundColorAttributeName);
196-
ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(ios.placeholder, colorAttibutes.copy());
193+
const ios = <UITextField>textBase._nativeView;
194+
const text = ios.placeholder + "";
195+
const colorAttributes = NSMutableDictionary.alloc().init();
196+
colorAttributes.setValueForKey(newValue, NSForegroundColorAttributeName);
197+
ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(text, colorAttributes.copy());
197198
}
198199

199200
private static resetPlaceholderColorProperty(textBase: View, nativeValue: any) {
200-
var ios = <UITextField>textBase._nativeView;
201-
let colorAttibutes = NSMutableDictionary.alloc().init();
202-
colorAttibutes.setValueForKey(nativeValue, NSForegroundColorAttributeName);
203-
ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(ios.placeholder, colorAttibutes.copy());
201+
const ios = <UITextField>textBase._nativeView;
202+
const text = ios.placeholder + "";
203+
const colorAttributes = NSMutableDictionary.alloc().init();
204+
colorAttributes.setValueForKey(nativeValue, NSForegroundColorAttributeName);
205+
ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(text, colorAttributes.copy());
204206
}
205207

206208
private static getNativePlaceholderColorValue(textBase: View): any {
207-
var ios = <UITextField>textBase._nativeView;
209+
const ios = <UITextField>textBase._nativeView;
210+
if (!ios.attributedPlaceholder) {
211+
return null;
212+
}
208213
return ios.attributedPlaceholder.attributeAtIndexEffectiveRange(NSForegroundColorAttributeName, 0, null);
209214
}
210215

0 commit comments

Comments
 (0)