SenlyCamile
Topic Author
Posts: 20
Joined: 15 Jan 2021, 07:38

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

28 Oct 2021, 03:36

I wanna to use DrawingContext to drawtext. I tried to get formattedtext from a textblock.But it did not work.
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

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

01 Nov 2021, 12:24

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));
}
 
SenlyCamile
Topic Author
Posts: 20
Joined: 15 Jan 2021, 07:38

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

02 Nov 2021, 04:33

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!

Who is online

Users browsing this forum: Bing [Bot] and 94 guests