Decorated and expanded text in a label

Is it possible to have bold and non-bold text inside a Xamarin.Forms Label widget? Is it possible to have ends inside a label?

<Label Text="Abc <b>def</b>"/>
<Label Text="Abc \ndef"/>

      

+3


source to share


2 answers


The best way is to bind the FormattedText style like this:

<Label FormattedText="{Binding CustomFormattedText}" />

      



And in the model:

    public FormattedString CustomFormattedText
    {
        get
        {
            return new FormattedString
            {
                Spans = {
                    new Span { Text = Sum, FontAttributes=FontAttributes.Italic, FontSize="10" },
                    new Span { Text = Info, FontSize="10" } }
            };
        }
        set { }
    }

      

+2


source


Take a look FormattedString

at the documentation .

You are creating an object FormattedString

that consists of several objects Span

, which can have their own font, weight, color, etc.

Example:



var labelFormatted = new Label ();
var fs = new FormattedString ();
fs.Spans.Add (new Span { Text="Red, ", ForegroundColor = Color.Red, FontSize = 20, FontAttributes = FontAttributes.Italic) });
fs.Spans.Add (new Span { Text=" blue, ", ForegroundColor = Color.Blue, FontSize = 32) });
fs.Spans.Add (new Span { Text=" and green!", ForegroundColor = Color.Green, FontSize = 12) });
labelFormatted.FormattedText = fs;

      

With respect to newline \n

should work. Another workaround would be to do it like this:

<Label>
<Label.Text>
This is my magical
unicorn text
</Label.Text>
</Label>

      

+1


source







All Articles