stepheaw
stepheaw

Reputation: 1713

Label wrapping with StackLayout

I'm using Xamarin and creating my view with XAML and I cannot for the life of me get this label to wrap the way I want it to. If the label gets to the edge of the screen, I want it to wrap like this...

This is the way I want it to look

Right now it is looking like this...

This is how it is showing up

Here is my code:

<StackLayout Orientation="Vertical" HorizontalOptions="StartAndExpand" BindingContext="{Binding CurrentProviderDetails}" Padding="20,20,20,20" >
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
<!--Certification Board-->
  <StackLayout Orientation="Horizontal" HorizontalOptions="StartAndExpand" Grid.Row="0" Grid.Column="0" >
    <Label Text="Certification Board: " FontSize="13" HorizontalOptions="Fill" VerticalOptions="CenterAndExpand" />
    <Label Text="{Binding Certification}" VerticalOptions="CenterAndExpand"  HorizontalOptions="Center" Font="17" LineBreakMode="WordWrap"/>
  </StackLayout>
 </Grid>
</StackLayout>

This doesn't need to be in a grid; this was just the method I was trying for now. My only requirement is that 'Certification Board' is a label, and I have to pass in a value that word wraps when it gets to the end of the screen. Any help would be awesome, Thanks!

Upvotes: 26

Views: 43120

Answers (3)

Thamarai T
Thamarai T

Reputation: 262

Add two columns to your grid and remove stacklayout around the labels, as shown below

<StackLayout Orientation="Vertical" HorizontalOptions="StartAndExpand" BindingContext="{Binding CurrentProviderDetails}" Padding="20,20,20,20" >
    <Grid>
           <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                 <ColumnDefinition Width="Auto" />
                 <ColumnDefinition Width="*" />
             </Grid.ColumnDefinitions>
             <!--Certification Board-->
             <Label Grid.Row="0" Grid.Column="0" Text="Certification Board: " FontSize="13" HorizontalOptions="FillAndExpand" />
             <Label Grid.Row="0" Grid.Column="1" Text="{Binding Certification}" HorizontalOptions="FillAndExpand" Font="17"/>
     </Grid>
</StackLayout>

You can more rows like this by adding like below

<Label Grid.Row="1" Grid.Column="0" Text="Certification Name: " FontSize="13" HorizontalOptions="FillAndExpand" />
<Label Grid.Row="1" Grid.Column="1" Text="{Binding CertificationName}" HorizontalOptions="FillAndExpand" Font="17"/>

Upvotes: 0

sarahr
sarahr

Reputation: 131

You can achieve the desired look by combining both of the Labels contained in your horizontally aligned StackLayout into a single Label and setting LineBreakMode="WordWrap". XAML has a great feature known as StringFormat. You can use this to prepend the static "Certification Board:" text to the bound Certification property. Your Label should look like this:

<Label Text="{Binding Certification, StringFormat='Board Certification:{0:F0}'}" LineBreakMode="WordWrap"/>

Upvotes: 13

Leonardo Lima Almeida
Leonardo Lima Almeida

Reputation: 379

Put a LineBreakMode="NoWrap" tag in your labels. This way you can avoid the wrap.

But if you don't have enough space, the word will be cut.

Upvotes: 28

Related Questions