Skip to content

Commit 79f65c7

Browse files
committed
fixes #107
1 parent b1873c2 commit 79f65c7

File tree

1 file changed

+113
-78
lines changed

1 file changed

+113
-78
lines changed

drop-down.ios.ts

Lines changed: 113 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
***************************************************************************** */
1616
import { Color } from "color";
17-
import { Label } from "ui/label";
1817
import { ItemsSource } from "ui/list-picker";
1918
import { Font } from "ui/styling/font";
19+
import { Style } from "ui/styling/style";
2020
import {
2121
TextAlignment,
2222
TextDecoration,
@@ -33,6 +33,7 @@ import {
3333
DropDownBase,
3434
Length,
3535
backgroundColorProperty,
36+
backgroundInternalProperty,
3637
colorProperty,
3738
fontInternalProperty,
3839
hintProperty,
@@ -179,13 +180,24 @@ export class DropDown extends DropDownBase {
179180
return this.nativeView.backgroundColor;
180181
}
181182
public [backgroundColorProperty.setNative](value: Color | UIColor) {
183+
if (!value) {
184+
return;
185+
}
186+
182187
const color = value instanceof Color ? value.ios : value;
183188

184189
this.nativeView.backgroundColor = color;
185190
this._listPicker.backgroundColor = color;
186191
this._listPicker.reloadAllComponents();
187192
}
188193

194+
public [backgroundInternalProperty.getDefault](): UIColor {
195+
return null;
196+
}
197+
public [backgroundInternalProperty.setNative](value: Color) {
198+
//
199+
}
200+
189201
public [fontInternalProperty.getDefault](): UIFont {
190202
return this.nativeView.font;
191203
}
@@ -196,6 +208,7 @@ export class DropDown extends DropDownBase {
196208

197209
public [textAlignmentProperty.setNative](value: TextAlignment) {
198210
switch (value) {
211+
case "initial":
199212
case "left":
200213
this.nativeView.textAlignment = NSTextAlignment.Left;
201214
break;
@@ -211,15 +224,15 @@ export class DropDown extends DropDownBase {
211224
}
212225

213226
public [textDecorationProperty.setNative](value: TextDecoration) {
214-
this._setTextAttributes();
227+
_setTextAttributes(this.nativeView, this.style);
215228
}
216229

217230
public [textTransformProperty.setNative](value: TextTransform) {
218-
this._setTextAttributes();
231+
_setTextAttributes(this.nativeView, this.style);
219232
}
220233

221234
public [letterSpacingProperty.setNative](value: number) {
222-
this._setTextAttributes();
235+
_setTextAttributes(this.nativeView, this.style);
223236
}
224237

225238
public [paddingTopProperty.setNative](value: Length) {
@@ -238,67 +251,6 @@ export class DropDown extends DropDownBase {
238251
this._setPadding({ left: layout.toDeviceIndependentPixels(this.effectivePaddingLeft) });
239252
}
240253

241-
public _setTextAttributes() {
242-
const style = this.style;
243-
const attributes = new Map<string, any>();
244-
245-
switch (style.textDecoration) {
246-
case "none":
247-
break;
248-
249-
case "underline":
250-
attributes.set(NSUnderlineStyleAttributeName, NSUnderlineStyle.StyleSingle);
251-
break;
252-
253-
case "line-through":
254-
attributes.set(NSStrikethroughStyleAttributeName, NSUnderlineStyle.StyleSingle);
255-
break;
256-
257-
case "underline line-through":
258-
attributes.set(NSUnderlineStyleAttributeName, NSUnderlineStyle.StyleSingle);
259-
attributes.set(NSStrikethroughStyleAttributeName, NSUnderlineStyle.StyleSingle);
260-
break;
261-
}
262-
263-
if (style.letterSpacing !== 0) {
264-
attributes.set(NSKernAttributeName, style.letterSpacing * this.nativeView.font.pointSize);
265-
}
266-
267-
if (this.nativeView.textColor && attributes.size > 0) {
268-
attributes.set(NSForegroundColorAttributeName, this.nativeView.textColor);
269-
}
270-
271-
const text: string = types.isNullOrUndefined(this.nativeView.text) ? "" : this.nativeView.text.toString();
272-
let sourceString: string;
273-
switch (style.textTransform) {
274-
case "uppercase":
275-
sourceString = NSString.stringWithString(text).uppercaseString;
276-
break;
277-
278-
case "lowercase":
279-
sourceString = NSString.stringWithString(text).lowercaseString;
280-
break;
281-
282-
case "capitalize":
283-
sourceString = NSString.stringWithString(text).capitalizedString;
284-
break;
285-
286-
default:
287-
sourceString = text;
288-
}
289-
290-
if (attributes.size > 0) {
291-
const result = NSMutableAttributedString.alloc().initWithString(sourceString);
292-
result.setAttributesRange(attributes as any, { location: 0, length: sourceString.length });
293-
this.nativeView.attributedText = result;
294-
}
295-
else {
296-
// Clear attributedText or text won't be affected.
297-
this.nativeView.attributedText = undefined;
298-
this.nativeView.text = sourceString;
299-
}
300-
}
301-
302254
private _setPadding(newPadding: { top?: number, right?: number, bottom?: number, left?: number }) {
303255
const nativeView = this.nativeView;
304256
const padding = nativeView.padding;
@@ -365,19 +317,40 @@ class DropDownListPickerDelegateImpl extends NSObject implements UIPickerViewDel
365317
// NOTE: Currently iOS sends the reusedView always as null, so no reusing is possible
366318
const owner = this._owner.get();
367319
const style = owner.style;
368-
const label = new Label();
369-
const labelStyle = label.style;
370-
320+
const label = TNSLabel.alloc().init();
321+
371322
label.text = owner._getItemAsString(row);
372323

373-
// Copy Styles
374-
labelStyle.color = style.color;
375-
labelStyle.fontInternal = style.fontInternal;
376-
labelStyle.padding = style.padding;
377-
labelStyle.textAlignment = style.textAlignment;
378-
labelStyle.textDecoration = style.textDecoration;
324+
// Copy Styles
325+
if (style.color) {
326+
label.textColor = style.color.ios;
327+
}
328+
329+
label.padding = {
330+
top: utils.layout.toDeviceIndependentPixels(owner.effectivePaddingTop),
331+
right: utils.layout.toDeviceIndependentPixels(owner.effectivePaddingRight),
332+
bottom: utils.layout.toDeviceIndependentPixels(owner.effectivePaddingBottom),
333+
left: utils.layout.toDeviceIndependentPixels(owner.effectivePaddingLeft)
334+
};
379335

380-
return label.ios;
336+
label.font = style.fontInternal.getUIFont(label.font);
337+
338+
switch (style.textAlignment) {
339+
case "initial":
340+
case "left":
341+
label.textAlignment = NSTextAlignment.Left;
342+
break;
343+
case "center":
344+
label.textAlignment = NSTextAlignment.Center;
345+
break;
346+
case "right":
347+
label.textAlignment = NSTextAlignment.Right;
348+
break;
349+
}
350+
351+
_setTextAttributes(label, style);
352+
353+
return label;
381354
}
382355

383356
public pickerViewDidSelectRowInComponent(pickerView: UIPickerView, row: number, component: number): void {
@@ -447,11 +420,12 @@ class TNSDropDownLabel extends TNSLabel {
447420
return this._hint;
448421
}
449422
set hint(value: string) {
423+
const owner = this._owner.get();
450424
this._hint = value;
451425

452426
if (!this._hasText) {
453427
this.text = value;
454-
this._owner.get()._setTextAttributes();
428+
_setTextAttributes(owner.nativeView, owner.style);
455429
}
456430
}
457431

@@ -465,13 +439,14 @@ class TNSDropDownLabel extends TNSLabel {
465439

466440
public setText(value: string) {
467441
const actualText = value || this._hint || "";
442+
const owner = this._owner.get();
468443

469444
this._hasText = !types.isNullOrUndefined(value) && value !== "";
470445
this.text = (actualText === "" ? " " : actualText); // HACK: If empty use <space> so the label does not collapse
471446

472447
this._refreshColor();
473448

474-
this._owner.get()._setTextAttributes();
449+
_setTextAttributes(owner.nativeView, owner.style);
475450
}
476451

477452
public becomeFirstResponder(): boolean {
@@ -523,4 +498,64 @@ class TNSDropDownLabel extends TNSLabel {
523498
private _refreshColor() {
524499
this.textColor = (this._hasText && this._internalColor ? this._internalColor : HINT_COLOR.ios);
525500
}
526-
}
501+
}
502+
503+
function _setTextAttributes(nativeView: TNSLabel, style: Style) {
504+
const attributes = new Map<string, any>();
505+
506+
switch (style.textDecoration) {
507+
case "none":
508+
break;
509+
510+
case "underline":
511+
attributes.set(NSUnderlineStyleAttributeName, NSUnderlineStyle.StyleSingle);
512+
break;
513+
514+
case "line-through":
515+
attributes.set(NSStrikethroughStyleAttributeName, NSUnderlineStyle.StyleSingle);
516+
break;
517+
518+
case "underline line-through":
519+
attributes.set(NSUnderlineStyleAttributeName, NSUnderlineStyle.StyleSingle);
520+
attributes.set(NSStrikethroughStyleAttributeName, NSUnderlineStyle.StyleSingle);
521+
break;
522+
}
523+
524+
if (style.letterSpacing !== 0) {
525+
attributes.set(NSKernAttributeName, style.letterSpacing * nativeView.font.pointSize);
526+
}
527+
528+
if (nativeView.textColor && attributes.size > 0) {
529+
attributes.set(NSForegroundColorAttributeName, nativeView.textColor);
530+
}
531+
532+
const text: string = types.isNullOrUndefined(nativeView.text) ? "" : nativeView.text.toString();
533+
let sourceString: string;
534+
switch (style.textTransform) {
535+
case "uppercase":
536+
sourceString = NSString.stringWithString(text).uppercaseString;
537+
break;
538+
539+
case "lowercase":
540+
sourceString = NSString.stringWithString(text).lowercaseString;
541+
break;
542+
543+
case "capitalize":
544+
sourceString = NSString.stringWithString(text).capitalizedString;
545+
break;
546+
547+
default:
548+
sourceString = text;
549+
}
550+
551+
if (attributes.size > 0) {
552+
const result = NSMutableAttributedString.alloc().initWithString(sourceString);
553+
result.setAttributesRange(attributes as any, { location: 0, length: sourceString.length });
554+
nativeView.attributedText = result;
555+
}
556+
else {
557+
// Clear attributedText or text won't be affected.
558+
nativeView.attributedText = undefined;
559+
nativeView.text = sourceString;
560+
}
561+
}

0 commit comments

Comments
 (0)