RTFData.StyleText color in Dark Mode and none DarkMode

My app write and read a text/string in/from a database field with type “Blob” into a TextArea. The styled text has no formats like color or bold etc. I add a value to this BLOB field with “sql” like this:
Var row As New DatabaseRow
if myDB.Connect then
// Notes
var s as new StyledText
s.Text = “My new text for a TextArea”
row.Column(“zBLOB”) = EncodeBase64(s.RTFData)
// Add to table
end if
Catch error As DatabaseException
MessageBox("The database file could not be created: " + error.Message)
End Try

If I read this value in my app with Dark Mode, the text has a black color. When I start the application without Dark Mode, the text in the TextArea is also in dark color.

If I create a new entry in my app and add text into the TextArea and save it, the text has a white color in Dark Mode and black without Dark Mode.

If I add “s.TextColor(0, s.Text.Length) = &c00000000” to the StyledText, the text in TextArea is now white in Dark Mode, but also white without Dark Mode.

How can I add text to a StyledText that has the right color for Dark Mode and none Dark Mode?

You may need to use NSColorMBS and NSAttributedStringMBS classes To benefit from automatic behavior.

RTF includes specific colors to use when rendering the text
There is no straight forward way of telling RTF to use the system default colors
Or you use something platform specific like NSAttributedString which can do this
Its more like specifying the color by name - TextColor - instead of as &c000000 or something

Like you I’m looking at something to deal with this
BUT this is not user input data so I do not have to worry about trying to mangle the RTF data etc to their needs
I can just walk the style runs and reapply the right colors by looking them up in the semantic colors appropriate for macOS, and Windows

The colors are stored in the {\\colortbl <… list of colors…>} segment.

Did you try to modify the black color [\red255\green255\blue255] (default) in Dark mode to white [\red0\green0\blue0], and the reverse in standard mode at Save or Read time ?

I don’t wanna create the RTF.Data manually. NSTextColorMBS with color name doesn’t solve the problem, so I add a light blue color that is good visible in Dark and none Dark mode.

If the text you want to save is just a string with no attributes, and you never want to have strings with attributes, then RTF and blobs are overkill. Just save the string and it will be rendered correctly in both light and dark mode.

No, that is not what I will have.

My app as a TextArea with Styled Text enabled. The application write and read RTFData with StyledTet from and into this TextArea. The application has a feature that can import values from a text file, (CSV, XML etc.) and also it can read data from an external sqlite database. For the import only I got a field from an external database (a BLOB field) or a string from a CSV or XML file. This value is stored by the import feature in my application. While this value is a string, I must convert it to a StyleText and save it in my database into a BLOB field as RTFData.

Sorry, I was not specific enough.

RTFData is text data (rtf commands and data).

So, with a search for {colortbl and a simple text replace, you will do the job.

Due to the late hour, I let you write the details (if you want).

Please giveme some details. What is the difference betweenthis way and a StyledText.Color value?

RTF: a Microsoft technologie, like html, this is a langage to code styled text.

StyledText is a Xojo technologie you are using in your project.

A Color in rtf is coded as I already shows above, for exmple for black it will be: \red255,\green255\blue255 while a StyledText Color is set differently.

At last, I do not say change the StyledText.Color value, but the color in the RTF {colortbl.

In the first case, you will probably have to make color changes n times (StyledText.Color), while in RTF you will have to make only once (for each color, in my xample once because the text color is black in the whole document).

Set a simple RTF document with only one color, and in your Xojo method, make a search/replace at read time depending on the current color mode (standard / dark) to this file before applying it to your TextArea.RTFData, then assign the new RTF to your TextArea.RTFData.

Now I cannot explain how to change that accordingly to the current mode (if the user change the color mode after you load the rtf) because I do not have programed that, And I do not found anything about DarkMode in Window Class nor Application, App and System.

Thank you for this information. Whits this I could resolve the problem.

var st as new StyledText
st.Text = “My Problem with StyledText”
var stNew as new StyledText
stNew.RTFData = st.RTFData.replace("{\colortbl\red0\green0\blue0;}\uc0", “{\colortbl\red255\green254\blue254;}\uc0 \f0\cf0\fs26”)

But there is always the problem, when the user import data when application is running in dark mode and later when application is not running In dark mode, the text is not visible.

For this reason, I import the text and set the color to light blue, so it can be read in dark and none dark mode. Also I add a context menu to the TextArea to change the color in one step:

select case hitItem.Tag
case “ResetColor”
var DefaultTAColor as color
if IsDarkMode then
DefaultTAColor = &cFFFEFE00
DefaultTAColor = &c00000000
end if
me.StyledText.TextColor(0, me.Text.Length) = DefaultTAColor

case “BlackToWhite”
me.StyledText.RTFData = me.StyledText.RTFData.replaceAll("\colortbl\red0\green0\blue0;}\uc0 \f0", “\colortbl\red255\green254\blue254;}\uc0 \f0\cf0”)

case “WhiteToBlack”
me.StyledText.RTFData = me.StyledText.RTFData.replaceAll("\colortbl\red255\green254\blue254", “\colortbl\red0\green0\blue0”)
end select

Thank you to all for resolving this problem.

At last, I found a page that display Dark Mode links:


I was searching DarkMode and nothing really helpful comes (and I was still a bit sleepy when I searched…) three entries only :wink:

I created a Feedback for that.

AppearanceChanged had to be DarkMode As Boolean :: True of False.

Been tired do not really explain why I do not saw that…

I recall I was searching for DarkMode.