标签中的字体加粗在Xamarin Forms中如何实现?

3
这是我的内容页标签:
<Label FontSize="Small"
       Grid.Row="0"
       Grid.Column="0"
       VerticalOptions="Center" 
       Text="{Binding ItemCode,StringFormat='Code: {0}'}">
</Label>

我想将粗体字应用于文本的这一部分“Code:”,在axml页面上有没有方法可以实现?

1
不使用自定义渲染器是不可能的。如果您不想创建自定义渲染器,则必须创建两个标签;一个用于“代码:”,您可以将其设置为粗体,另一个用于ItemCode的任何值。 - sme
2个回答

8
您可以使用FormattedText属性来设置文本的不同部分。就像这样:
<Label Grid.Row="0"
       Grid.Column="0"
       FontSize="Small"
       VerticalOptions="Center" >
    <Label.FormattedText>
        <FormattedString>
            <FormattedString.Spans>
                <Span Text="Code: "
                      FontAttributes="Bold"/>
                <Span Text="{Binding ItemCode}" />
            </FormattedString.Spans>
        </FormattedString>
    </Label.FormattedText>
</Label>

2

Xamarin.Forms使用XAML而非AXML

请使用两个分开的Label标签:

<Label FontSize="Small" 
       Grid.Row="0" 
       Grid.Column="1" 
       VerticalOptions="Center" 
       FontAttributes="Bold"
       Text="Code :">
</Label>

<Label FontSize="Small" 
       Grid.Row="0" 
       Grid.Column="1" 
       VerticalOptions="Center" 
       Text="{Binding ItemCode,StringFormat='{0}'}">
</Label>

编辑:将其包装在具有Horizontal方向的StackLayout中,它们将位于同一“行”上。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接