How do I center multiline text in a label - xamarin.forms

Has anyone figured out how to center (or justify or in any way horizontally effect) text in a Xamarin Forms Label with LineBreakMode=WordWrap? Without using a WebView (or similar)?
All HorizontalOptions entries are cheerfully ignored in this code snippet:
<StackLayout>
<Label
Margin="75,0,75,0"
FontAttributes="Italic"
FontSize="Large"
HorizontalOptions="Center"
LineBreakMode="WordWrap"
Text="Live life as though nobody is watching, and express yourself as though everyone Is listening."
VerticalOptions="CenterAndExpand" />
</StackLayout>
It looks like this.

You should use HorizontalTextAlignment="Center" in this case.
<StackLayout>
<Label
Margin="75,0,75,0"
FontAttributes="Italic"
FontSize="Large"
HorizontalOptions="Center"
HorizontalTextAlignment="Center"
LineBreakMode="WordWrap"
Text="Live life as though nobody is watching, and express yourself as though everyone Is listening."
VerticalOptions="CenterAndExpand" />
</StackLayout>

add this line in your code
HorizontalTextAlignment="Center"
<StackLayout>
<Label
Margin="75,0,75,0"
FontAttributes="Italic"
FontSize="Large"
HorizontalOptions="Center"
HorizontalTextAlignment="Center"
LineBreakMode="WordWrap"
Text="Live life as though nobody is watching, and express yourself as though everyone Is listening."
VerticalOptions="CenterAndExpand" />
</StackLayout>

Related

Xamarin Forms CarouselView and ScrollView already scrolled without visited

I have a CarouselView with inside a ScrollView .
I've seen it plastered across documentation that it's not advisable to nest a scrollview inside any other scrollable layout, but what else can I do when I need to be able to scroll between items and each item must have scrollable content also.
Now my problem is with iOS (with all version after 10+) and Android (will all version after 6+). Let's assume we have 10 items and during the first display I make a vertical scroll in the middle of the content. When I get to the fifth view it is not displayed as a new element and therefore from the top but in the middle. If I do it to the second, the sixth mirrors the second and so on. In practice it seems that the views are linked by 5 in 5.
Here is my simple code:
<CarouselView ItemsSource="{Binding Monkeys}"
HorizontalScrollBarVisibility="Never" >
<CarouselView.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Vertical" Spacing="0">
<ScrollView VerticalScrollBarVisibility="Never" HorizontalScrollBarVisibility="Never" >
<StackLayout>
<Image
Source="{Binding ImageUrl}" HeightRequest="350"
Aspect="AspectFill" />
<Label Text="{Binding Name}"
LineBreakMode="TailTruncation"/>
<Label Text="{Binding Details}" FontSize="35" />
<Label Text="{Binding Details}" FontSize="35" TextColor="Aqua"/>
<Label Text="{Binding Details}" FontSize="35" TextColor="Fuchsia"/>
<Label Text="{Binding Details}" FontSize="35" TextColor="OliveDrab"/>
</StackLayout>
</ScrollView>
<Label Text="Fixed Button" BackgroundColor="Red" TextColor="White" HeightRequest="50" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" />
</StackLayout>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
GIF dimostration

Xamarin.forms Change Language at runtime

I have a login page and my code is this:
<ContentPage.Content>
<Grid>
<StackLayout Orientation="Vertical" Padding="20">
<Entry Placeholder="{x:Static resources:AppResources.EMB_PLACEHOLDER}" Keyboard="Numeric" Text="{Binding EMB}"></Entry>
<Button Text="{x:Static resources:AppResources.LOGIN_BTN}" TextColor="White" Padding="0,-20" BackgroundColor="#07987f" Command="{Binding LoadLoginCommand}"></Button>
<Label
Margin="0,10,0,0" >
<Label.FormattedText>
<FormattedString>
<Span Text="{x:Static resources:AppResources.MESSAGE} "></Span>
</FormattedString>
</Label.FormattedText>
</Label>
<Grid Margin="0,10,0,0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Padding="20,0,0,0" Text="{x:Static resources:AppResources.LBLLANGUAGE}" VerticalOptions="Center" FontSize="Body" TextColor="Gray" Grid.Column="0"/>
<Picker x:Name="languagePicker" Grid.Column="1" ItemsSource="{Binding Languages}" Title="{x:Static resources:AppResources.LBLLANGUAGE}" ItemDisplayBinding="{Binding Name}" SelectedItem="{Binding SelectedLanguage, Mode=TwoWay}"></Picker>
</Grid>
</StackLayout>
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<ActivityIndicator IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}" HorizontalOptions="Center" VerticalOptions="Center"></ActivityIndicator>
<Label Text="{x:Static resources:AppResources.TSMSG1}" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" HorizontalOptions="Center" VerticalOptions="Center" IsVisible="{Binding IsBusy}"/>
</StackLayout>
</Grid>
</ContentPage.Content>
Have a picker with list of languages. Also have resx file for every language. My scenario is when i click on picker and select some language i want to change the language of all text on the login page in choosed language in realtime. Any idea how to do it?
The first link provided in the comments is an awesome solution, however for the shake of simplicity, a very simple way of doing what you want is to bind the control's text to properties in the ViewModel:
<Label Text="{Binding TSMSG1}"...
rather than using
<Label Text="{x:Static resources:AppResources.TSMSG1}"...
Then in your ViewModel you would have a property TSMSG1 (and others) that you manually update when the language changes. Of course those properties need to follow INPC pattern for the changes to be reflected on the view.
I would not recommend doing this everywhere because bindings are expensive for your app performance, but it should be fine for the screen that changes the language.

Xamarin Forms group controls without modifying layout

I've got following view:
<ContentPage.Content>
<AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<StackLayout AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1">
<StackLayout VerticalOptions="CenterAndExpand" Margin="20, 0, 20, 0">
<StackLayout>
<Label Text="Login" HorizontalOptions="CenterAndExpand" />
<Entry Text="{Binding Login, Mode=OneWayToSource}"></Entry>
</StackLayout>
<StackLayout>
<Label Text="Hasło" HorizontalOptions="CenterAndExpand" />
<Entry IsPassword="True" Text="{Binding Password, Mode=TwoWay}"></Entry>
</StackLayout>
<Button Text="Zaloguj się" Command="{Binding SignInCommand}"></Button>
</StackLayout>
</StackLayout>
<BoxView AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1" BackgroundColor="Gray" Opacity="0.5" InputTransparent="false" IsVisible="{Binding IsBusy}" />
<ActivityIndicator IsRunning="{Binding IsBusy}" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" />
</AbsoluteLayout>
</ContentPage.Content>
I want to move BoxView and ActivityIndicator to external control to make reusable component. The problem is that to achieve this I need to "group" these controls to obtain one child element.
The question is if is there any element I can use to group my controls, but which will not affect the way how controls are displayed? Alternatevlly which element I can use and how to still have effect of overlay over whole page and loading indicator?
I was trying to use AbsoluteLayout, StackLayout etc. but I couldn't position it to persist initial effect (overlay on whole page and loading indicator in the center with opacity = 1).
You can use DependencyService and reuse it in any page.
Check this tutorial.
There is also a working sample.

How to set WidthRequest=50 to button in Xamarin.Forms for Windows Phone?

WidthRequest=50 in Windows phone project not working. In android and iOS it is working. But in WinPhone displaying only half button. I tried with MinimumWidthRequest also, that also not working.
Button takes MinimumWidth of only 109 in windows phone. Less than 109 WIdthRequest, disappears overflowed width.
Does anybody knows how to modify this MinimumWidthRequest in WinPhone? or Any other solution for this?
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<Label Text="WidthRequest=109" HorizontalTextAlignment="Center" Margin="0,10,0,0"/>
<Button HorizontalOptions="Center" VerticalOptions="Center" Text="109" HeightRequest="50" WidthRequest="109"/>
<Label Text="WidthRequest=108" HorizontalTextAlignment="Center" Margin="0,10,0,0"/>
<Button HorizontalOptions="Center" VerticalOptions="Center" Text="108" HeightRequest="50" WidthRequest="108"/>
<Label Text="WidthRequest=70" HorizontalTextAlignment="Center" Margin="0,10,0,0"/>
<Button HorizontalOptions="Center" VerticalOptions="Center" Text="70" HeightRequest="50" WidthRequest="70"/>
<Label Text="WidthRequest=50" HorizontalTextAlignment="Center" Margin="0,10,0,0"/>
<Button HorizontalOptions="Center" VerticalOptions="Center" Text="50" HeightRequest="50" WidthRequest="50"/>
</StackLayout>
This is how Button displays in Windows Phone
This is how Button displays in Android
Finally solved the problem, Created CustomRender of Button and defined Control.MinWidth for Button. MinimumWidthRequest not working as expected in Xamarin.Forms.

Xamarin forms card layout using xaml

I am trying to build Xaml for Xamarin , I am able to get the UI But the label is not getting binded. (Android Platform)
Also tried Text="{Binding Name}" for label. But not getting any labels rendered. Do i need to write up any additional code ?
<ContentPage.Content>
<ListView SelectedItem="{Binding SelectedResult}" x:Name="listview1" ItemsSource="{Binding QueryResults}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame Padding="0,0,0,10">
<Frame.Content>
<Frame Padding="15,15,15,15" HeightRequest="36" OutlineColor="Aqua" BackgroundColor="Blue">
<Frame.Content>
<StackLayout Padding="20,0,0,0" Orientation="Horizontal" HorizontalOptions="StartAndExpand">
<Label Text="label text for test" TextColor="Yellow">
</Label>
</StackLayout>
</Frame.Content>
</Frame>
</Frame.Content>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
It worked for me.
Here is the code:
`
<Frame Padding="15,15,15,15" HeightRequest="36" OutlineColor="Aqua" BackgroundColor="Blue">
<Frame.Content>
<StackLayout Padding="20,0,0,0" Orientation="Horizontal" HorizontalOptions="StartAndExpand">
<Label Text="{Binding Name}" TextColor="Yellow">
</Label>
</StackLayout>
</Frame.Content>
</Frame>
`
There was frame inside frame so it was overlapping.
Hope this helps!
Even though very late reply after 3 months :p

Resources