Eto.Drawing.FormattedText set bold text?

Hello,

How do I set a specific portion of text to be bold?

I am formatting some text together where I want the variable “text” to be drawn as bold and the variable “shortcut” to be drawn as regular…

            self.label_text = f"{text}\n      {shortcut}" if shortcut else text

            # Create the formatted text with wrapping
            self.formatted_label_text = Eto.Drawing.FormattedText()
            self.formatted_label_text.Alignment = Eto.Drawing.FormattedTextAlignment.Left
            self.formatted_label_text.Font = UI.label_font
            self.formatted_label_text.ForegroundBrush = Eto.Drawing.SolidBrush(t_color)
            self.formatted_label_text.MaximumSize = Eto.Drawing.SizeF(310, 100)
            self.formatted_label_text.Text = self.label_text
            self.formatted_label_text.Trimming = Eto.Drawing.FormattedTextTrimming.WordEllipsis
            self.formatted_label_text.Wrap = Eto.Drawing.FormattedTextWrapMode.Word

Thanks for the help!

If It’s for a label I don’t believe this is possible as it just has a string for its Text property and doesn’t utilise Rtf.

The FormattedText class is for Drawables / Graphics afaik.

RichTextArea has Rich Text Formatting for this kind of thing

Sorry I didn’t clarify, yes it is for Drawable.

Thank you this looks like what I need ^

Question, the methods all say “selected text” does that mean it only effects the text the user has highlighted such as during editing a text field or by selected does it mean the string that is being passed in the argument?

Thanks for the help!

This would be my guess, I have not confirmed myself though :slight_smile:

1 Like

Hi @michaelvollrath,

below is an example for the RichTextArea which i made look like a label so it does not allow to select the text, has no border or background. You’ll also need to set the font size in the rtf text string which seems to be 9.0 for labels on my system. The rtf code looks like this:

"{\rtf1 \qc \fs18 \b This is bold text \b0 this is normal text\par}"

Everything between \b and \b0 will be bold. Everything between \qc and \par will be horizontally centered. The \fs18 sets the font size to 9.0. Color is set through properties:

ETO_RichTextArea.py (1.9 KB)

I’ve never used Eto.Drawing.FormattedText() but if you draw only two words, why not draw them seperately with different fonts ? (one bold, one not bold).

_
c.

2 Likes

Mostly because while the example I posted about here only has the two words, there are cases where additional formatting is needed such as underline, italics, strikethrough etc. and it gets dynamically generated so the syntax formatting you provided is actually perfect for this.

Thank you so much, I hadn’t played with the RTF area much yet so I really appreciate the sample code!

1 Like

Hi @michaelvollrath,

just for completeness. It is possible in a drawable using Eto.Drawing.FormattedText too, but you’ll have to import FontWeights.Bold from PresentationCore.dll for this to work and apply it to the ControlObject:

TestFormattedText.py (1.6 KB)

the important part is this:

formatted_text.ControlObject.SetFontWeight(FontWeights.Bold, 0, 5)
e.Graphics.DrawText(formatted_text, Eto.Drawing.PointF(0,0))

more info is found here
_
c.

2 Likes