I've got a nativescript app. I have a situation were I need to display text dynamically, so I don't know how much text and how much lines it will be.
The text need to wrap over multi lines and has to be aligned in center horizontally (not vertically, simply same distance to left and right) always.
Therefore I guess <Label> isn't the right element, because it is not for multiline (if I got this right?!).
So I choose <TextView>, but here the styles text-align: center got ignored.
So in documentation I found constructor textAlignment https://docs.nativescript.org/api-reference/modules/_ui_text_base_#textalignment, but I don't get it to work.
Doesn't work:
<TextView text="{{ taskString }}"
horizontalAlignment="center"
editable="false"
></TextView>
Doesn't work:
<TextView text="{{ taskString }}"
textAlignment="center"
editable="false"
></TextView>
Please let me know, what obvious I didn't get here. Thx.
Upate:
Label with textWrap="true" normally works just fine. But I have a "complex" <ContentView> <FlexboxLayout> combination, that seems to cause the problem with the height of Label that doesn't get updated.
Solution in my case:
Just don't use a <FlexboxLayout> <FlexboxLayout> ... </FlexboxLayout> </FlexboxLayout>solution. That won't calculate the height of Label dynamically.
Nativescript Playground:
https://play.nativescript.org/?template=play-ng&id=SnNmkQ
Label can be multiline if you enable textWrap
XML
<Label text="{{ taskString }}" textWrap="true" ...
Or
CSS
Label {
white-space: normal;
}
Related
I have this sample code that works as I want:
<ScrollView orientation="horizontal">
<GridLayout columns="*,*,*,*,*,*" >
<Label class="gridlabel" col="0" text="Monday" />
<Label class="gridlabel" col="1" text="Tuesday" />
<Label class="gridlabel" col="2" text="Wednesday" />
<Label class="gridlabel" col="3" text="Thursday" />
<Label class="gridlabel" col="4" text="Friday" />
<Label class="gridlabel" col="5" text="Saturday" />
</GridLayout>
</ScrollView>
That is, the labels within the GridLayout scroll horizontally.
I have a component that generates a GridLayout, and now I need to wrap that in a horizontal ScrollView.
That is, I for each label:
let label = new Label();
// Add tap event handler for each tab
label.on("tap", function () {
onTabTap(label, "tabTap");
}.bind(label));
label.id = key;
label.text = key;
label.class = "gridtab";
this.addColumn(new ItemSpec(1, GridUnitType.STAR));
GridLayout.setColumn(label, i);
GridLayout.setRow(label, 0);
this.addChild(label);
But when I try to add the ScrollView, I get errors. If I try to add the labels to the ScrollView, such as scrollView.addChild(label) (where scrollView is an instance of ScrollView), I get "scrollView.addChild is not a function". (See this similar SO post). If, as suggested in the mentioned post, I use scrollView.content = this; then I get the error, Error: View already has a parent.
So, the question is, from code, how do I replicate the hierarchy from my sample xml? That is, how can I wrap the generated GridLayout in a horizontal ScrollView?
Edit 7/17/2020
Upon reflection, I don't think this can work given my component's current design. That is, it subclasses GridLayout, and I want the generated GridLayout to be wrapped by a ScrollView, but that would be external to the content generated by the component, yes? It almost seems I'd need to subclasss ScrollView, and then generate the GridLayout within.
So, I was ultimately able to resolve this by subclassing StackLayout, then within the StackLayout adding a ScrollView, and within the ScrollView adding the GridLayout. The "magic" is:
scroll.content = grid;
this.addChild(scroll);
Where scroll is the ScrollView instance, and grid is the GridLayout instance.
Then, after spending a day on this I found I didn't actually need horizontal scrolling after all, but at least I know what to do should the need arise.
I have a RadListView for a prop, totalMovies, which is an array. Each movie contains an image field and favorite field. If the favorite field is false, then an unfilled heart image shows and there's an event that fires on tap that changes that movie's favorite to true. After clicking the heart, I see in console that it registers and I verified again with VueTools that totalMovies shows the movies I favorites as having favorite: true, but the image that shows is always the unfilled-heart image. I am guessing or assuming that the RadListView is not refreshing properly?
<RadListView
for="(movie,index) in totalMovies"
#itemTap="onItemTap($event)"
itemHeight="80"
:key="index"
gridSpanCount=1
>
<v-template>
<FlexboxLayout class="item-row" :key="index" flexDirection="row" width="100%" height="100%">
<Image v-if="movie.favorite" width="20" src="~/assets/images/heart-filled.png" />
<Image v-else #tap="handleToggleFavorite(movie)" width="20" src="~/assets/images/heart-unfilled.png" />
</FlexboxLayout>
</v-template>
</RadListView>
EDIT: Adding playground: http://play.nativescript.org/?template=play-vue&id=GNo11S&v=2
ObservableArray listens to changes on array index and refreshes the list view. So if you are using ObservableArray, try to update item at index using setItem method.
Otherwise in your case simple array should work as Vue can detect the changes.
Using NativeScript 3 + Angular 5.
I need to allow the user to swipe an item within a RadListView to reveal a short description about the item.
<RadListView
[items]="featuredVideos"
pullToRefresh="true"
selectionBehavior="None"
(itemSwipeProgressStarted)="onSwipeCellStarted($event)"
swipeActions="true"
(pullToRefreshInitiated)="onPullToRefreshInitiated($event)">
<ng-template tkListItemTemplate let-item="item">
<VideoComponent [video]="item"></VideoComponent>
</ng-template>
<ng-template tkListItemSwipeTemplate let-item="item">
<GridLayout columns="*, 500" class="gridLayoutLayout">
<StackLayout id="short-desc" col="1">
<Label [text]="item.shortDescription" class="body" verticalAlignment="center" horizontalAlignment="center"></Label>
</StackLayout>
</GridLayout>
</ng-template>
</RadListView
I would like to be able to access the current item inside the tkListItemSwipeTemplate so that I can bind the text property of the label to the short description. Currently I am getting the following error
JS: ERROR TypeError: Cannot read property 'shortDescription' of undefined
I know this question is a little old now, but for anyone who comes here looking for an answer, I have managed to work-around this problem. Bind your label text to a different value i.e.:
<Label [text]="myLabelText"...
Then in your onSwipeCellStarted callback, you can use the index property on the ListViewEventData argument and set 'myLabelText' appropriately e.g.:
onSwipeCellStarted(args: ListViewEventData) {
...
this.myLabelText = featuredVideos[args.index].shortDescription;
}
This should get you out of trouble. Hope it helps :)
I have the following structure:
<ScrollView tkMainContent>
<ListView [items]="students$ | async" class="list-group" *ngIf="students$">
<ng-template let-student="item">
<StackLayout>Student details go here</StackLayout>
I'm not able to show a button inside the ScrollView when there is no student in my list.
How can I still show the button?
Note: I'm testing on a real iOS device.
<FlexboxLayout flexDirection="column">
<GridLayout class="page-content" id="placeholderLayout" visibility="{{ hasContent ? 'collapse' : 'visible' }}">
<Label class="page-icon fa" text=""></Label>
<Label class="page-placeholder" style="white-space: normal" text="Click the camera button to add image"></Label>
</GridLayout>
<ScrollView>
<-- List View Here -->
</ScrollView>
</FlexboxLayout>
I use something like this on NS Core, to show placeholder content. The way to set visibility might be different in angular, but a similar markup should work for you.
In the component.ts, you should take care to evaluate if there is content to show in list view, if there are, then set hasContent to true, and false otherwise.
Hope that helps :) let me know if you face any trouble while implementing this.
I have two buttons with a label in between. When a button is tapped, the label text changes, but it appears to have a static width set to the width of the initial text. When the text gets longer, part of it is replaced with an ellipses.
The code is pretty simple.
<StackLayout orientation="horizontal">
<Button text="<" tap="onPreviousLevel"></Button>
<Label id="title" text="{{ name }}"></Label>
<Button text=">" tap="onNextLevel"></Button>
</StackLayout>
Setting the horizontal alignment for labels seems to make them responsive (change based on content).
In CSS this would be
#title {
horizontal-align: center;
}
Try this:
<Label id="title" text="{{ name }}" textwrap="true"></Label>
Another way, in iOS:
var myLabel = page.getViewById("title");
myLabel.ios.numberOfLines = 2;
Not sure if it would be appropriate in this case, but in mine, ridiculously adding a "width:auto;" style override to the label pretty much did the trick, so far preventing the string to truncate and get the ellipsis at the end.
For example, a given TextField input (and probably many other components) with a "width:xx%;" sitting next the label will stretch or compress accordingly, depending on the label's string length, and the component's remaining space in a given container such as a horizontally oriented StackLayout.
For NativeScript Angular:
<Button text="Some label" horizontalAlignment="center" />
The Button will then only take the width that it needs, like fit-content would usually do. I have no technical explanation for that - it is just some NativeScript magic (if you could call it so..)
Source