Up to date
This page is up to date for Godot
If you still find outdated information, please open an issue.
BBCode in RichTextLabel¶
Label nodes are great for displaying basic text, but they have limitations. If you want to change the color of the text, or its alignment, you can only do that to the entire label. You can't make a part of the text have another color, or have a part of the text centered. To get around these limitations, you would use a RichTextLabel.
RichTextLabel allows for complex formatting of text using a markup syntax or the built-in API. It uses BBCodes for the markup syntax, a system of tags that designate formatting rules for a part of the text. You may be familiar with them if you ever used forums (also known as bulletin boards, hence the "BB" in "BBCode").
Unlike Label, RichTextLabel also comes with its own vertical scrollbar. This scrollbar is automatically displayed if the text does not fit within the control's size. The scrollbar can be disabled by unchecking the Scroll Active property in the RichTextLabel inspector.
Note that the BBCode tags can also be used to some extent in the XML source of the class reference. For more information, see Class reference primer.
You can see how BBCode in RichTextLabel works in action using the Rich Text Label with BBCode demo project.
By default, RichTextLabel functions like a normal Label. It has the property_text property, which you can edit to have uniformly formatted text. To be able to use BBCode for rich text formatting, you need to turn on the BBCode mode by setting bbcode_enabled. After that, you can edit the text property using available tags. Both properties are located at the top of the inspector after selecting a RichTextLabel node.
BBCode [color=green]test[/color] would render the word "test" with
a green color.
Most BBCodes consist of 3 parts: the opening tag, the content and the closing
tag. The opening tag delimits the start of the formatted part, and can also
carry some configuration options. Some opening tags, like the
shown above, also require a value to work. Other opening tags may accept
multiple options (separated by spaces within the opening tag). The closing tag
delimits the end of the formatted part. In some cases, both the closing tag and
the content can be omitted.
Unlike BBCode in HTML, leading/trailing whitespace is not removed by a RichTextLabel upon display. Duplicate spaces are also displayed as-is in the final output. This means that when displaying a code block in a RichTextLabel, you don't need to use a preformatted text tag.
[tag]content[/tag] [tag=value]content[/tag] [tag option1=value1 option2=value2]content[/tag] [tag][/tag] [tag]
RichTextLabel doesn't support entangled BBCode tags. For example, instead of using:
Handling user input safely¶
In a scenario where users may freely input text (such as chat in a multiplayer
game), you should make sure users cannot use arbitrary BBCode tags that will be
parsed by RichTextLabel. This is to avoid inappropriate use of formatting, which
can be problematic if
[url] tags are handled by your RichTextLabel (as players
may be able to create clickable links to phishing sites or similar).
[rb] tags, we can replace the opening and/or
closing brackets of any BBCode tag in a message with those escaped tags. This
prevents users from using BBCode that will be parsed as tags – instead, the
BBCode will be displayed as text.
The above image was created using the following script:
extends RichTextLabel func _ready(): append_chat_line("Player 1", "Hello world!") append_chat_line("Player 2", "Hello [color=red]BBCode injection[/color] (no escaping)!") append_chat_line_escaped("Player 2", "Hello [color=red]BBCode injection[/color] (with escaping)!") # Returns escaped BBCode that won't be parsed by RichTextLabel as tags. func escape_bbcode(bbcode_text): # We only need to replace opening brackets to prevent tags from being parsed. return bbcode_text.replace("[", "[lb]") # Appends the user's message as-is, without escaping. This is dangerous! func append_chat_line(username, message): append_text("%s: [color=green]%s[/color]\n" % [username, message]) # Appends the user's message with escaping. # Remember to escape both the player name and message contents. func append_chat_line_escaped(username, <