Xamarin(XAML)如何并排放置2个标签

Jon*_*Jon 4 c# xamarin.forms

我有2个标签需要使用不同的字体来制作单个标签"My Company(c)"(copywrite symbol)."我的公司"将是一个大字体,'(c)'是一个小字体.我不能让它们作为单个标签出现.似乎存在间距问题.我尝试了以下内容.

   <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>
Run Code Online (Sandbox Code Playgroud)

但它看起来像" 我的公司(空间)(c) "

任何想法如何使它看起来像"我的公司(c)",总是在同一条线上和在一起?

Ada*_*ley 8

还有另一种方法,但是您不能直接为文本分配样式,但是可以选择许多字体选项。您仍然可以将样式设置为主标签。

<Label>
    <Label.FormattedText>
        <FormattedString>
            <Span Text="Company" FontAttributes="Bold"/>
            <Span Text=" ©" FontSize="Micro" />
        </FormattedString>
    </Label.FormattedText>
</Label>
Run Code Online (Sandbox Code Playgroud)

如果要绑定,则需要创建一个Converter,该Converter返回FormattedString,然后分配给FormattedText。如果要重用具有不同样式的参数,可以创建带参数的Converter。

<Label FormattedText="{Binding Text, Converter={StaticResource FormattedStringConverter}}" />
Run Code Online (Sandbox Code Playgroud)


Tai*_*T's 7

您可以在以下位置指定Spacing属性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>
Run Code Online (Sandbox Code Playgroud)

默认情况下,该值为6.


loa*_*ger 5

您应该使用 VerticalTextAlignment 属性并将其设置为居中。您还应该将两者的标签边距设置为 0。

\n\n
<StackLayout Grid.Row="1" Orientation="Horizontal">\n    <Label\n        x:Name="lbCo" \n        Text="My Company"\n        VerticalTextAlignment="Center"\n        Style="{DynamicResource LargeLabel}"/>\n    <Label\n        x:Name="lbcopywrite" \n        Text="\xc2\xa9"\n        VerticalTextAlignment="Center"\n        Style="{DynamicResource SmallLabel}"/>\n</StackLayout>\n
Run Code Online (Sandbox Code Playgroud)\n