Set backColor for specific words in a TextArea

Hello,
I’m trying to make an editor in Xojo. I need to change the background color of some specific words.
For example, if the TextArea (desktop version, for Windows and OSX) contains:

My name is Alessandro

I wish to highlight the name “Alessandro” by setting its background color as yellow (just an example).
Is it possible? How?

Thank you for your help!

I don’t know about Windows, but in OS X you can use the MacOSLib TextAreaExtension FontBackgroundColor. You supply the offset, the length, and the color.

Thank you Jonathan. Based on your suggestion I made a search, and seems there is nothing similar in Windows. So it is Mac-dependent.
I need a way to make it in windows and mac (my app is cross-platform).
Thank you

I just made a test. I created a simple text, in Windows, using WordPad for Windows.
Using that program I set the background color to red (RTF format).
Then I simply copied and pasted that WordPad text into a TextArea and… wow! It worked! My text in the textarea had a red background for a single word!
So it means there should be a way to change the color of a single word, but seems there is no method exposed to do that via Xojo directly :frowning:

UPDATE 2: If I read “RTFData” in StyledText I do NOT get information about the text word color, even if I see the color in the component.
For example: I create the text

My name is Alessandro

and the word “Alessandro” is RED.

My RTF source is:

{\\rtf1\\ansi\\ansicpg1252{\\fonttbl{\\f0\\fnil Calibri;}}{\\colortbl\\red0\\green0\\blue0;}\\uc0 \\ql\\f0\\fs48 My name is Alessandro\\par \\fs28 \\par }

I see in TextArea component the word Alessandro in red, but in RTF source the red color does not appear :frowning:
The only color in RTF source is the black color (text “My name is” has black color… so black color is the standard “ink” color in my text).

{\\colortbl\\red0\\green0\\blue0;}

EDIT: This is the original RTF source (the correct source generated from Windows Wordpad):

{\\rtf1\\ansi\\ansicpg1252\\deff0\ ouicompat\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Calibri;}} {\\colortbl ;\\red0\\green255\\blue0;\\red255\\green0\\blue0;} {\\*\\generator Riched20 10.0.10586}\\viewkind4\\uc1 \\pard\\sa200\\sl276\\slmult1\\highlight1\\f0\\fs36\\lang9 My name is \\highlight2 Alessandro\\highlight1\\par \\highlight0\\fs22\\par }

TextArea does not implement PostScript fully. There are things that are simply ignored, like subscript and superscript . Chances are it is also the case for text BackColor.

See a similar thread : https://forum.xojo.com/29922-highlight-text-in-a-textarea/0

[quote=268909:@Michel Bujardet]TextArea does not implement PostScript fully. There are things that are simply ignored, like subscript and superscript . Chances are it is also the case for text BackColor.

See a similar thread : https://forum.xojo.com/29922-highlight-text-in-a-textarea/0[/quote]

Yes, you agree. I regret such functionality was not implemented in TextArea, I think it could be very important and useful.

Thank you again.

I use the Lexing plugin from Alfred van Hoek. Works great.

Thank you I will check it immediately :slight_smile:

Thank you for this suggestion, I will check it now!

[quote=268889:@Alessandro Manotti]I just made a test. I created a simple text, in Windows, using WordPad for Windows.
Using that program I set the background color to red (RTF format).
Then I simply copied and pasted that WordPad text into a TextArea and… wow! It worked! My text in the textarea had a red background for a single word!
So it means there should be a way to change the color of a single word, but seems there is no method exposed to do that via Xojo directly :-([/quote]

Alessandro, you can do that, just check the StyledText and StyleRun classes.

Do you have an example of highlighting using Xojo’s built-in styleRun? The docs don’t list highlighting. It would be cool if there was some sort of undocumented feature, but you would think others would have found it by now.

Textarea StyledText does not support the text backcolor property that would enable such outline.

Even if pasting such style is possible via the keyboard in both Windows and Mac, there is no way to access the property with Xojo, save it or load it. A text created with TextEdit or Notepad containing outlined text, for instance, will be stripped of that property when loaded in a TextArea through StyledText.RTFData.

Thank you for your suggestion, but even that class does NOT support color background (or, as some people call it, “text highlight”).
That class only contain text color, font, etc…

[quote=269243:@Michel Bujardet]Textarea StyledText does not support the text backcolor property that would enable such outline.

Even if pasting such style is possible via the keyboard in both Windows and Mac, there is no way to access the property with Xojo, save it or load it. A text created with TextEdit or Notepad containing outlined text, for instance, will be stripped of that property when loaded in a TextArea through StyledText.RTFData.[/quote]

I think, since TextArea is a very good component, it could be great if Xojo could implement color background as cross-platform feature.

Why don’t you file a feature request ?

Good idea but… how? It’s the first time I use Xojo so I don’t know how I can do it.
Please can you give me some hints?
Thank you very much for your help!

I think it could be possible, if one works with low-level code, SDK platform specific.
I use several programming languages, and I can say there are some cross platform GUI toolkit that implement such feature :slight_smile:

[quote=269262:@Alessandro Manotti]Good idea but… how? It’s the first time I use Xojo so I don’t know how I can do it.
Please can you give me some hints?
Thank you very much for your help![/quote]

Go to Xojo.com and download the Feedback application from the Extras folder. Install it.

When you launch Feedback, enter the title of your suggestion or bug report, press enter. Feedback will show you all similar reports it found. If nothing seems appropriate, click “Create case”, the fill the fields and click Submit.

About possible implementation, at least in Mac and Windows it shows when pasting text with background set. Unfortunately, no way to save that or load it. It should not be extremely difficult to implement.

Maybe MBS can modify its RTFDataMBS class to support it. For now, a quick test does not work with an rtf saved from TextEdit.

[quote=269265:@Michel Bujardet]Go to Xojo.com and download the Feedback application from the Extras folder. Install it.

When you launch Feedback, enter the title of your suggestion or bug report, press enter. Feedback will show you all similar reports it found. If nothing seems appropriate, click “Create case”, the fill the fields and click Submit.

About possible implementation, at least in Mac and Windows it shows when pasting text with background set. Unfortunately, no way to save that or load it. It should not be extremely difficult to implement.

Maybe MBS can modify its RTFDataMBS class to support it. For now, a quick test does not work with an rtf saved from TextEdit.[/quote]
Just done! Thank you!