2017-05-25 62 views
2

我有2個標籤需要使用不同的字體來製作單個標籤 「My Company(c)」(copywrite symbol)。 「我的公司」將是一個大字體,'(c)'是一個小字體。我無法讓它們顯示爲單個標籤。似乎有間距問題。我已經嘗試了以下。Xamarin(XAML)如何並排放置2個標籤

<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> 

但現在看來,像「我公司(空格)(C)

任何想法如何可以使它看起來像「我的公司(C)」,總是在同一直線上,共同?

回答

3

還有另外一種方法,但你不能直接指定樣式的文字,但你可以選擇衆多的字體選項。儘管如此,您仍然可以將Style設置爲主標籤。

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

如果你想綁定,你需要創建一個Converter,它返回一個FormattedString,並分配給FormattedText。如果您想重複使用不同的樣式,可以創建一個帶參數的Converter。

<Label FormattedText="{Binding Text, Converter={StaticResource FormattedStringConverter}}" /> 
+0

很高興知道,不知道你可以使用跨文本。下次會嘗試。謝謝 – Jon

2

您可以在StackLayout指定Spacing屬性:

<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> 

默認情況下,該值是6

+0

最簡單的解決方案 – Jon

1

您應該使用VerticalTextAlignment屬性,並將其設置爲中心。兩者都應該將標籤邊距設置爲0。

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