Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jogboms/8c6d22e8db81ca33cade64cdc9a0848b to your computer and use it in GitHub Desktop.
Save jogboms/8c6d22e8db81ca33cade64cdc9a0848b to your computer and use it in GitHub Desktop.
Directive for NativeScript-angular, adding the property maxLines to Label
import { Directive, ElementRef, Input, OnInit, OnChanges } from '@angular/core';
import { Label } from 'ui/label';
@Directive({
selector: '[maxLines]',
})
export class LabelMaxLinesDirective implements OnInit, OnChanges {
@Input('maxLines') public maxLines: number = 1;
public get nativeView(): Label {
return this.el.nativeElement;
}
constructor(private el: ElementRef) {}
public ngOnInit() {
const nativeView = this.nativeView;
if (nativeView instanceof Label) {
nativeView.on(Label.loadedEvent, () => {
this.applyMaxLines();
});
}
}
public ngOnChanges(changes: any) {
if (changes.maxLines) {
this.applyMaxLines();
}
}
private applyMaxLines() {
const nativeView = this.nativeView;
const maxLines = Math.max(Number(this.maxLines) || 0, 1);
if (nativeView.android) {
nativeView.android.setMaxLines(maxLines);
} else if (nativeView.ios) {
nativeView.ios.numberOfLines = maxLines;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment