文本的不同的颜色在同一个标​​签使用串格式

时间:2018-02-01 05:54:34

标签: xamarin xamarin.forms

我有一个xaml代码如下:

 <Label Text="{Binding Id,StringFormat='{0} *'}" HorizontalOptions="StartAndExpand" HorizontalTextAlignment="Center">

这里,这可能是Id是一种颜色而*是另一种颜色吗?

2 个答案:

答案 0 :(得分:1)

您可以使用FormattedText,如下所示:

<Label HorizontalTextAlignment="Center">
    <Label.FormattedText>
        <FormattedString>
            <Span Text="" TextColor="Red"/>
            <Span Text="" TextColor="Blue"/>
        </FormattedString>
    </Label.FormattedText>
</Label>

这个问题是它不支持绑定。但是,您可以从后面的页面代码设置文本。如果它不适合您,只需在水平StackLayout中使用两个标签。

<强>更新

现在跨度是可绑定的! https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/label#formatted-text

答案 1 :(得分:0)

在Xamarin.IOS中 像这样使用

var attributedText = new NSMutableAttributedString(“在“ + HowManySecondsString +”中完成它以达到”); var range = attributedText.MutableString.LocalizedStandardRangeOfString(new NSString(HowManySecondsString)); attributedText.AddAttribute(UIStringAttributeKey.ForegroundColor,UIColor.Red,range); label.AttributedText = attributedText;