Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin (XAML) how to place 2 labels side by side

I have 2 labels that need to use different fonts to make single label this "My Company (c)" (copywrite symbol). "My Company " will be a Large font and the '(c)' a small font. I can't get them to appear as 1 single label. There seems to be spacing issues. I have tried the following.

   <StackLayout Grid.Row="1" Orientation="Horizontal">
                  <Label
                      x:Name="lbCo" 
                      Text="My Company"
                      Style="{DynamicResource LargeLabel}"/>

                  <Label
                      x:Name="lbcopywrite" 
                      Text="©"
                      Margin="0,-7,0,0"
                      Style="{DynamicResource SmallLabel}"/>
                 </StackLayout>

But it appears like "My Company (spaces) (c)"

Any ideas how can make it look like "My Company(c)", always on the same line and together?

like image 255
Jon Avatar asked Nov 28 '22 08:11

Jon


2 Answers

There is another way, but you can't assign a Style directly to the text, but you can choose numerous font options. You can still set a Style to the main label though.

<Label>
    <Label.FormattedText>
        <FormattedString>
            <Span Text="Company" FontAttributes="Bold"/>
            <Span Text=" ©" FontSize="Micro" />
        </FormattedString>
    </Label.FormattedText>
</Label>

If you want binding, you need to create a Converter, that returns a FormattedString, and assign to FormattedText. You could create a Converter with Parameters if you want to reuse it, with different styles.

<Label FormattedText="{Binding Text, Converter={StaticResource FormattedStringConverter}}" />
like image 61
Adam Avatar answered Dec 04 '22 20:12

Adam


You can specify the Spacing property on the StackLayout:

<StackLayout Grid.Row="1" Orientation="Horizontal" Spacing="0">
     <Label x:Name="lbCo" 
            Text="My Company"
            Style="{DynamicResource LargeLabel}"/>

     <Label x:Name="lbcopywrite" 
            Text="©"
            Margin="0,-7,0,0"
            Style="{DynamicResource SmallLabel}"/>
</StackLayout>

By default, the value is 6.

like image 28
TaiT's Avatar answered Dec 04 '22 21:12

TaiT's