How to make words bold within text

валера карманов 396 Reputation points
2024-10-20T10:51:38.4633333+00:00

I need to make "{0}" and "${1:F2}" bold selection. Is it possible to do this?

You have {0} unpaid invoices totaling ${1:F2}.

Developer technologies .NET .NET MAUI
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2024-10-21T05:48:23.83+00:00

    Hello,

    Maui provides two ways to bold text on a page using a Label control. One is through HTML, and the other is through FormattedString.

    You could refer to the following code snippet and official documentation to understand this workaround.

    Workaround 1: Html Type:

    Step 1. Define a Label control on the page, or you can create a Label class in the code-behind.

    <Label x:Name="bold_sample_label"/>
    

    Step 2. Use Html formatting text to bold specific content in a string.

    int value1 = 0;
    decimal value2 = 9.02m;
    bold_sample_label.Text = string.Format($"You have <span ><strong>{0}</strong></span> unpaid invoices totaling <span ><strong>${1:F2}</strong></span>.", value1, value2);
    bold_sample_label.TextType = TextType.Html;
    

    Workaround 2: FormattedString:

    Step 1. The same as Step 1 of Workaround 1.

    Step 2. Divide the string into sections, add content and styles, and assign values ​​to Label.

    FormattedString formattedString = new FormattedString();
    formattedString.Spans.Add(new Span { Text = "You have ",  });
    formattedString.Spans.Add(new Span { Text = value1.ToString(), FontAttributes = FontAttributes.Bold});
    formattedString.Spans.Add(new Span { Text = " unpaid invoices totaling " });
    formattedString.Spans.Add(new Span { Text = string.Format($"${1:F2}.",value2), FontAttributes = FontAttributes.Bold });
    bold_sample_label.FormattedText = formattedString;
    

    Documentation:

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.