Drawing multiline styled text into canvas

Dear all,

I would like to draw into a Canvas several lines of text.
Each line is composed by 3 parts of text, with different font colors.
And I would like to prepare the full text in a method, for then drawing it all together at once into the Canvas.
I would you approach this?
I tried to use “TextShape” but it seems you cannot combine different TextShapes into one …
Thanks a lot !

PS: I know that I could use a text area, but it is not what I need.

Have you looked at:


Your canvas has a graphics property in its Paint event.

Thanks time. Indeed I read that already.
But how can I combine different “text styles” in the same line, and combine multiple text lines?
And if possible “build” the text in a subroutine and then send it to the pain event of canvas …
Thanks again !

You have to do all the actual writing in the Paint event in one go. You would have to create a structure describing your text and the text itself and then fire the Paint event which would decode your structure and draw the text. That seems a lot of work.

In my case I used a DesktopTextArea, enabled Styled Text, and used StyleRuns. Still quite a bit of work and made more complex for me because I wanted two columns, meaning I couldn’t use the TextArea’s scrollbar and had to account for the height of all the text, as well as manage a scrollbar too.

The problem with DesktopTextArea is that (at least on Mac) it cannot be completely transparent …

BBCode label I wrote a few years ago should help you

The control isn’t maintained anymore. If you use it please send me a PM and I’ll send you the source code.

1 Like

If that is canvas-based, I would love to try it. I promise not to share with anybody without your permission.
Thanks !

This StyledTextPrinter replacement I made might help:

Yes it is canvas based. Not the new DesktopCanvas but it should be fairly easy to update.

This is a recent regression. TextArea transparency works fine pre-Xojo2022.

Dear all,

thanks a lot for the suggestions and for sharing interesting code.
At the end, I opted for making a 2-dimensional array of TextShape, where each row represent a paragraph, and it is composed by 3 columns which contain the 3 text parts of styled text.
Then in the paint event I just load any information contained in this 2-dimensional array.
It seems to work, (I also realised that I dont need the canvas, I can directly use the paint event of the window), but I jut need to be sure if it is energy (=CPU) efficient.
Thanks again to all.