Page 1 of 1

Is there any demo about how to use the class FormattedText?

Posted: 28 Oct 2021, 03:36
by SenlyCamile
I wanna to use DrawingContext to drawtext. I tried to get formattedtext from a textblock.But it did not work.

Re: Is there any demo about how to use the class FormattedText?

Posted: 01 Nov 2021, 12:24
by sfernandez
Hi, to draw a text using a FormattedText first provide the text or inlines that will generate the text runs, and then call the layout method to place the text according to the provided bounds and font properties.
void OnRender(DrawingContext* context)
{
  mFormattedText->BuildTextRuns(mText.Str(), nullptr, mFont, FontWeight_Normal, FontStretch_Normal, FontStyle_Normal,
    12.0f, 0.0f, nullptr, mForegroundBrush, nullptr, TextDecorations_None, 1);
  mFormattedText->Layout(TextAlignment_Left, TextWrapping_Wrap, TextTrimming_None, mRenderSize.width, mRenderSize.height,
    0.0f, LineStackingStrategy_MaxHeight);
  context->DrawText(mFormattedText, Rect(mRenderSize));
}

Re: Is there any demo about how to use the class FormattedText?

Posted: 02 Nov 2021, 04:33
by SenlyCamile
Hi, to draw a text using a FormattedText first provide the text or inlines that will generate the text runs, and then call the layout method to place the text according to the provided bounds and font properties.
void OnRender(DrawingContext* context)
{
  mFormattedText->BuildTextRuns(mText.Str(), nullptr, mFont, FontWeight_Normal, FontStretch_Normal, FontStyle_Normal,
    12.0f, 0.0f, nullptr, mForegroundBrush, nullptr, TextDecorations_None, 1);
  mFormattedText->Layout(TextAlignment_Left, TextWrapping_Wrap, TextTrimming_None, mRenderSize.width, mRenderSize.height,
    0.0f, LineStackingStrategy_MaxHeight);
  context->DrawText(mFormattedText, Rect(mRenderSize));
}
Thanks!