Android Label not auto resizing after value change

android

#1

Dear reader,

I’ve created an issue topic on github about the bug that Label introduces for Android.
Please checkout my topic and hope we can solve this issue.

Kind regards,

btaluy


#2

Looks like a platform parity issue, you may fix it by simply calling .requestLayout() on textChange event of label.


#3

Hallo Manojdcoder, cheers for your reply, I’m gonna test it out and come back to you with results. :slight_smile:


#4

That bug is very annoying.

If you’re using nativescript-angular, you can use this directive to workaround the problem.

import { Directive, ElementRef, Input } from '@angular/core';
import { Label } from 'tns-core-modules/ui/label';

/**
 * Make sure the layout is updated then the Label's text is changed.
 */
@Directive({
  // tslint:disable-next-line:directive-selector
  selector: 'Label[text]',
})
export class UpdateLabelDirective {
  @Input('text')
  public set _text(text: string) {
    const nativeView = this.nativeView;
    if (nativeView && nativeView instanceof Label) {
      nativeView.text = text;
      nativeView.requestLayout();
    }
  }

  private get nativeView(): Label {
    return this.el.nativeElement;
  }

  constructor(private readonly el: ElementRef) {}
}

EDIT:
Gist: https://gist.github.com/m-abs/7743a0aa41621a5a54a74ef9d357e41b