Fixed(IOS): Setting placeholder color on text filed crashes if no hint is set

This commit is contained in:
vakrilov
2016-12-05 15:16:21 +02:00
parent fee1dd9a59
commit 8101a68c25
2 changed files with 34 additions and 19 deletions

View File

@ -576,3 +576,13 @@ export function test_set_placeholder_color() {
TKUnit.assertEqual(actualColorHex, expectedColorHex); 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<viewModule.View>) {
view.setInlineStyle("placeholder-color: " + expectedColorHex + ";");
let actualColorHex = textFieldTestsNative.getNativePlaceholderColor(view).hex;
TKUnit.assertEqual(actualColorHex, expectedColorHex);
});
}

View File

@ -7,7 +7,7 @@ import * as style from "ui/styling/style";
import {View} from "ui/core/view"; import {View} from "ui/core/view";
function onSecurePropertyChanged(data: PropertyChangeData) { function onSecurePropertyChanged(data: PropertyChangeData) {
var textField = <TextField>data.object; const textField = <TextField>data.object;
textField.ios.secureTextEntry = data.newValue; textField.ios.secureTextEntry = data.newValue;
} }
@ -156,24 +156,24 @@ export class TextField extends common.TextField {
} }
public _onHintPropertyChanged(data: PropertyChangeData) { public _onHintPropertyChanged(data: PropertyChangeData) {
var textField = <TextField>data.object; const textField = <TextField>data.object;
textField.ios.placeholder = data.newValue + ""; textField.ios.placeholder = data.newValue + "";
} }
} };
export class TextFieldStyler implements style.Styler { export class TextFieldStyler implements style.Styler {
private static setColorProperty(view: View, newValue: any) { private static setColorProperty(view: View, newValue: any) {
var tf: UITextField = <UITextField>view._nativeView; const tf: UITextField = <UITextField>view._nativeView;
TextFieldStyler._setTextFieldColor(tf, newValue); TextFieldStyler._setTextFieldColor(tf, newValue);
} }
private static resetColorProperty(view: View, nativeValue: any) { private static resetColorProperty(view: View, nativeValue: any) {
var tf: UITextField = <UITextField>view._nativeView; const tf: UITextField = <UITextField>view._nativeView;
TextFieldStyler._setTextFieldColor(tf, nativeValue); TextFieldStyler._setTextFieldColor(tf, nativeValue);
} }
private static _setTextFieldColor(tf: UITextField, newValue: any) { private static _setTextFieldColor(tf: UITextField, newValue: any) {
var color: UIColor = <UIColor>newValue; const color: UIColor = <UIColor>newValue;
if ((<any>tf).isShowingHint && color) { if ((<any>tf).isShowingHint && color) {
tf.textColor = (<UIColor>color).colorWithAlphaComponent(0.22); tf.textColor = (<UIColor>color).colorWithAlphaComponent(0.22);
} }
@ -184,27 +184,32 @@ export class TextFieldStyler implements style.Styler {
} }
private static getNativeColorValue(view: View): any { private static getNativeColorValue(view: View): any {
var tf: UITextField = <UITextField>view._nativeView; const tf: UITextField = <UITextField>view._nativeView;
return tf.tintColor; return tf.tintColor;
} }
// placeholder-color // placeholder-color
private static setPlaceholderColorProperty(textBase: View, newValue: any) { private static setPlaceholderColorProperty(textBase: View, newValue: any) {
let ios = <UITextField>textBase._nativeView; const ios = <UITextField>textBase._nativeView;
let colorAttibutes = NSMutableDictionary.alloc().init(); const text = ios.placeholder + "";
colorAttibutes.setValueForKey(newValue, NSForegroundColorAttributeName); const colorAttributes = NSMutableDictionary.alloc().init();
ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(ios.placeholder, colorAttibutes.copy()); colorAttributes.setValueForKey(newValue, NSForegroundColorAttributeName);
ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(text, colorAttributes.copy());
} }
private static resetPlaceholderColorProperty(textBase: View, nativeValue: any) { private static resetPlaceholderColorProperty(textBase: View, nativeValue: any) {
var ios = <UITextField>textBase._nativeView; const ios = <UITextField>textBase._nativeView;
let colorAttibutes = NSMutableDictionary.alloc().init(); const text = ios.placeholder + "";
colorAttibutes.setValueForKey(nativeValue, NSForegroundColorAttributeName); const colorAttributes = NSMutableDictionary.alloc().init();
ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(ios.placeholder, colorAttibutes.copy()); colorAttributes.setValueForKey(nativeValue, NSForegroundColorAttributeName);
ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(text, colorAttributes.copy());
} }
private static getNativePlaceholderColorValue(textBase: View): any { private static getNativePlaceholderColorValue(textBase: View): any {
var ios = <UITextField>textBase._nativeView; const ios = <UITextField>textBase._nativeView;
if (!ios.attributedPlaceholder) {
return null;
}
return ios.attributedPlaceholder.attributeAtIndexEffectiveRange(NSForegroundColorAttributeName, 0, null); return ios.attributedPlaceholder.attributeAtIndexEffectiveRange(NSForegroundColorAttributeName, 0, null);
} }