From 8101a68c25ab462301a574606945b5945ba4f03e Mon Sep 17 00:00:00 2001 From: vakrilov Date: Mon, 5 Dec 2016 15:16:21 +0200 Subject: [PATCH] Fixed(IOS): Setting placeholder color on text filed crashes if no hint is set --- tests/app/ui/text-field/text-field-tests.ts | 10 +++++ .../ui/text-field/text-field.ios.ts | 43 +++++++++++-------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/tests/app/ui/text-field/text-field-tests.ts b/tests/app/ui/text-field/text-field-tests.ts index 619061fa0..aec9d4f72 100644 --- a/tests/app/ui/text-field/text-field-tests.ts +++ b/tests/app/ui/text-field/text-field-tests.ts @@ -575,4 +575,14 @@ export function test_set_placeholder_color() { let actualColorHex = textFieldTestsNative.getNativePlaceholderColor(view).hex; TKUnit.assertEqual(actualColorHex, expectedColorHex); }); +} + +export function test_set_placeholder_color_when_hint_is_not_set() { + let view = new textFieldModule.TextField(); + let expectedColorHex = "#ffff0000"; + helper.buildUIAndRunTest(view, function (views: Array) { + view.setInlineStyle("placeholder-color: " + expectedColorHex + ";"); + let actualColorHex = textFieldTestsNative.getNativePlaceholderColor(view).hex; + TKUnit.assertEqual(actualColorHex, expectedColorHex); + }); } \ No newline at end of file diff --git a/tns-core-modules/ui/text-field/text-field.ios.ts b/tns-core-modules/ui/text-field/text-field.ios.ts index a2968b778..f23667742 100644 --- a/tns-core-modules/ui/text-field/text-field.ios.ts +++ b/tns-core-modules/ui/text-field/text-field.ios.ts @@ -7,7 +7,7 @@ import * as style from "ui/styling/style"; import {View} from "ui/core/view"; function onSecurePropertyChanged(data: PropertyChangeData) { - var textField = data.object; + const textField = data.object; textField.ios.secureTextEntry = data.newValue; } @@ -46,7 +46,7 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate { owner.dismissSoftInput(); - if (owner.formattedText){ + if (owner.formattedText) { owner.formattedText.createFormattedStringCore(); } } @@ -86,8 +86,8 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate { } } } - - if (owner.formattedText){ + + if (owner.formattedText) { owner.formattedText._updateCharactersInRangeReplacementString(range.location, range.length, replacementString); } } @@ -156,24 +156,24 @@ export class TextField extends common.TextField { } public _onHintPropertyChanged(data: PropertyChangeData) { - var textField = data.object; + const textField = data.object; textField.ios.placeholder = data.newValue + ""; } -} +}; export class TextFieldStyler implements style.Styler { private static setColorProperty(view: View, newValue: any) { - var tf: UITextField = view._nativeView; + const tf: UITextField = view._nativeView; TextFieldStyler._setTextFieldColor(tf, newValue); } private static resetColorProperty(view: View, nativeValue: any) { - var tf: UITextField = view._nativeView; + const tf: UITextField = view._nativeView; TextFieldStyler._setTextFieldColor(tf, nativeValue); } private static _setTextFieldColor(tf: UITextField, newValue: any) { - var color: UIColor = newValue; + const color: UIColor = newValue; if ((tf).isShowingHint && color) { tf.textColor = (color).colorWithAlphaComponent(0.22); } @@ -184,27 +184,32 @@ export class TextFieldStyler implements style.Styler { } private static getNativeColorValue(view: View): any { - var tf: UITextField = view._nativeView; + const tf: UITextField = view._nativeView; return tf.tintColor; } // placeholder-color private static setPlaceholderColorProperty(textBase: View, newValue: any) { - let ios = textBase._nativeView; - let colorAttibutes = NSMutableDictionary.alloc().init(); - colorAttibutes.setValueForKey(newValue, NSForegroundColorAttributeName); - ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(ios.placeholder, colorAttibutes.copy()); + const ios = textBase._nativeView; + const text = ios.placeholder + ""; + const colorAttributes = NSMutableDictionary.alloc().init(); + colorAttributes.setValueForKey(newValue, NSForegroundColorAttributeName); + ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(text, colorAttributes.copy()); } private static resetPlaceholderColorProperty(textBase: View, nativeValue: any) { - var ios = textBase._nativeView; - let colorAttibutes = NSMutableDictionary.alloc().init(); - colorAttibutes.setValueForKey(nativeValue, NSForegroundColorAttributeName); - ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(ios.placeholder, colorAttibutes.copy()); + const ios = textBase._nativeView; + const text = ios.placeholder + ""; + const colorAttributes = NSMutableDictionary.alloc().init(); + colorAttributes.setValueForKey(nativeValue, NSForegroundColorAttributeName); + ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(text, colorAttributes.copy()); } private static getNativePlaceholderColorValue(textBase: View): any { - var ios = textBase._nativeView; + const ios = textBase._nativeView; + if (!ios.attributedPlaceholder) { + return null; + } return ios.attributedPlaceholder.attributeAtIndexEffectiveRange(NSForegroundColorAttributeName, 0, null); }