TextArea styled text (RTF) and dark mode

Has anyone solved the problem of using TextArea styled text and dark mode? I have the MBS plugins and can get the RTF from a textarea with an expandedcolortbl, which should allow automatic switching from black text to white text and vice versa. But when I set the RTF of a textarea, the color of the styled text is “fixed” to the color used in the mode in which it was edited (e.g. when created in dark mode, the text remains white when used in light mode). I know that the Formatted Text Control is supposed to handle this, but I want to stick with the native textarea.

It’s been many year since I did any work with RTF, but I recall that the format takes absolute colors.

The Xojo TextArea is a native control on the macOS NSTextView, so I’d recommend searching for how other macOS devs have solved this.

Hi there
Textarea normally always has black as the text color
The light or dark mode then switches on correctly
White / black or black / white
The Color.SelectedFromDialog (c, “Select Color”) box is always with
Text area connected. if a color is chosen in the box
the text color is available in the text area without further assignment.

just set the function in Textarea.open:

Dim c As Color = &c000000
Dim b As Boolean
b= Color.SelectedFromDialog(c,“Select Color”)

And so, each time you open the window, you have to set a color ?

Sam got it, but perhaps I phrased the problem poorly. RTF has the text color baked in. If the color of the text is black, it’s black in both light and dark mode. So if you create styled text and save as RTF in light mode, it’s black (invisible) in dark mode. And vice versa – create the RTF in dark mode and the text is white. Switch to light mode and it’s still white.

I found that the solution to this is to use an extension to RTF in which there is a second text color table, called expandedcolortbl, that will be used if you’re in dark mode. Here is a nice explanation

link text

This expandedcolortbl table is actually already created by functions that use macOS APIs, including the MBS plugin RTFDataMBS. However, when I save the RTF created by that method, switch modes, and load that RTF to a TextArea, the text color is incorrect. That is, it’s still the color (black or white) of the mode in which it was created.

After more experimenting, I think the answer is that would be extremely difficult to support dark mode if you use RTF. It involves the use of an internal table called expandedcolortbl, as noted above. This table is generated but not filled by functions like the MacOSLib RTFValue or RTFDataMBS. Moreover, even if you fill it yourself the references to color in the body of the RTF have to be changed to deal with the updated table (e.g. changing \cf0 to \cf2). I’m sure all of this could be done, but it would be tricky and prone to failure. I note that Nisus Writer Pro, which uses RTF as its file format, and which is written by an RTF wizard, isn’t dark mode compliant.

Hello Emile,
Hello Jonathan
here is my solution for the dark and light mode text area
is written in API2

https://www.dropbox.com/s/778n37xt9she7zc/RTF-Test.xojo_binary_project?dl=0

Thanks for this, but I’m not sure it addresses the problem. I type in some styled text in dark mode and save the RTF. I then open the saved RTF in light mode. It’s white, not black, and I get the error message that “Text color white not visible”. What should I be doing so that it appears as black in light mode.?

hello jonathan,
I don’t change my texts manually by selecting the text area with the mouse and then choosing the color in the color box below

it also works with the following code, which would have to be inserted into the program

Var c As Color
For i As Integer = 0 To textarea1.value.Length
TextArea1.SelectionStart = i
TextArea1.SelectionLength = 1
c= textarea1.SelectionTextColor
If c.red > 250 And c.green > 250 And c.blue > 250 Then // black = c.red < 5 and c.green < 5 and c.blue < 5
textarea1.SelectionTextColor = Color.black // or white
End If
Next

Thanks again. Yes, I’ve been playing with a similar approach but but integrating through style runs rather than each character. It’s not very satisfactory, however.

To bypass the html page (for direct downloading), replace the ending 0 by 1 and that’s it:

https://www.dropbox.com/s/778n37xt9she7zc/RTF-Test.xojo_binary_project?dl=1

Nice interface (with some German texts), but:

Enlarge the window, then change some buttons properties.

Styles (On / Off) Buttons are capricious

Load: allow to select anything. Choosing an RTFD file leads to a crash. Do not load .txt files.
Save: the .rtf extension is missing (maybe only the dot ?).

Also, when I select the text and apply a color, the selected text is deselected (on purpose, in Canvas2.MouseDown, strange).

What text editor are-you using to write your code ?
(why are there “spaces” at the left and right of some commands in the source code ?)

[quote=472295:@Jonathan Ashwell]
Thanks again. Yes, I’ve been playing with a similar approach but but integrating through style runs rather than each character. It’s not very satisfactory, however.[/quote]
Was looking up how to add annotations to RTF in a NSTextView, when I came across the following document.
https://developer.apple.com/documentation/macos_release_notes/macos_mojave_10_14_release_notes/appkit_release_notes_for_macos_10_14?language=objc

There’s several paragraphs on RTF and theme colors (Search for “Rich Text Authoring”). It appears using declares to set the text colors to the color constants and again declares to save and load the styled text, should give you what you want.

Thanks, @Sam Rowlands This does indeed seem to be a solution that I’ll look at. For now I solved this by passing the styled text of the field through a converter that turns stylers of black text to white, and then back again if the RTF is saved to the database. It works, but it’s not elegant.