Labels with highlighted words

Hello,
I’m trying to make a card with description written on it. Some words shloud be highlighted in some way (different color and/or bold letters). I’m using labels to display the text. The card itself is a gameobject, and I’d prefer to keep it that way. Is there any way to achive that?

I’m aware that there is Ritch Text library by britzl, which does all that and much much more, but it’s only for GUI.

You’ll have to build it yourself with multiple labels. Use label.get_text_metrics() to get the sizes of things so you know how to piece the parts together. You can use a game object factory to spawn and place the label parts as needed when text changes. Don’t worry about creating / destroying as needed it’s fast.

2 Likes

I’ve had plans to add support for game objects and label components in RichText for a while but haven’t really had time to do it yet.

You have to do it manually for now I’m afraid.

1 Like

Thanks, will try that. I’ll post the code after im done.

1 Like

I’m not sure Rich Text is possible with labels right now because label.set_text() is not synchronous, so you can’t call label.set_text() followed by a label.get_text_metrics() in quick succession the way Rich Text does.

1 Like

That is not that big of a problem. I’m using two functions for this, one that creates all the labels and fills them with text, and posts a message, calling another function that will position the text. Thanks to the way that the message system works, it ensures that the second function is called after the label.set_text() is done doing it’s thing.

I’m almost done with my code, I’ll post it here soon.

1 Like

Here it is:
image
Generated using following message:
msg.post("printer", "text", {text = "This {important}text consist of {green}multiple labels.", width = 350})

The the code contains very simple styling function, using keywords to change label properties. You cannot stack keywords. The styling function only knows important and green keywords.

Here is the zip containig the code and all the game objects you’ll need to generate this text:
printer.zip (1.7 KB)

EDIT:

I made some mistakes in the code I posted.

  1. I didn’t reset the styles and urls tables.
  2. I forgot the parent of the words
  3. the tables with words shloud be inside self
6 Likes

Good job!

It is possible to reupload zips to fix changes here / upload fixed versions.

1 Like

I’ve decided to create a repository for this:


This is not a library, it’s just code snippets and examples.
I know that GitLab is not as popular for public repositories, but it’s what I have been using for a while, so I’m hosting it there. Feel free to DM me here on the forum instead of making an issue.

5 Likes