Random beginner-question: apply BbCode to RichTextLabel inside script (not in Inspector)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pferft

Hi everyone,

it appears fairly easy to apply some BbCode to (parts of) a text in a RichTextLabel via the editor’s Inspector, like right-aligning [right]{text}[/right].

But what if I don’t have any straight text to deal with in the Inspector but rather some yet to be inserted input (by the user) in an empty RichTextLabel? How could I mark certain elements of a text (e. g. entered numbers) to only tweak them?
And I’d like to apply a general right-aligning to the whole RichTextLabel, but as there is no such option to select in the Inspector, isn’t there any way to apply this by code within the node’s script?

Could you maybe make a Word doc or something demonstrating what you’re trying to achieve and share a screenshot? It’s a little hard for me to imagine what you’re trying to accomplish.

DDoop | 2020-12-15 16:02

But of course, I’ll try: let’s say I have an empty text-field made of a RichTextLabel (gray). The user then enters a couple of numbers, a comma and some more numbers. What I’d like to achieve is three things:

  1. the numbers are aligned to the right of the box
  2. spacing of the thousands’ position is a little farther from the hundreds’ than usual (but not an empty space!)
  3. the numbers after the comma should be a little smaller and positioned higher in the line

Which could look somewhat like this:

WeTransfer - Send Large Files & Share Photos Online - Up to 2GB Free

Telling the Label that the current input is a thousands’ position or an after-comma one is a challenge of its own, but I believe I could manage that. I have, however, no idea, how I can BbCode these parts accordingly, so that they „automatically“ do it when these positions are entered. Any thought is much appreciated!

pferft | 2020-12-15 20:51

:bust_in_silhouette: Reply From: jgodfrey

Typically, you would not use a RichTextLabel for user input as it’s a read-only control. While you could listen for user input and update the contents as needed, I doubt that’s really what you want to do.

Instead, I’d guess you might want to use a TextEdit control, which does allow user input and will allow you to vary the color of the input based on the add_keyword_color and add_color_region functions.

You can sort of equate it to a syntax-highlighting programmer’s editor. With the proper configuration, it can highlight dynamic text based on your needs.

Here’s a short video that goes over some of these features:

Thanks, this is really helpful (and straight forward).
So I see three ways of assigning a color to a chosen sign: via code, via Custom Color in the editor or via Theme/Color. (After a quick test with both code and Theme attached, it appears that the first entry respects the code, but then the Theme kicks in - perhaps it’s a good idea to choose one of the options and stick to it.)

Now I wonder how far that approach can reach beyond setting colors! I’d love such a simple code-line approach to make all kinds of tweaks possible - font size, alignment, spacing, transparency etc…
Trying to have text aligned to the right, I can see smooth_scrolling and wrap_enabled and such things, but nothing concerning aligning… (A Label simply features Aligning in the Editor - is there a reason why TextEdit (or RichTextLabel as well) doesn’t?)

pferft | 2020-12-17 10:56