A control for displaying text that can contain different font styles, images, and basic formatting.
A control for displaying text that can contain custom fonts, images, and basic formatting. [RichTextLabel] manages these as an internal tag stack. It also adapts itself to given width/heights.
[b]Note:[/b] Assignments to [member text] clear the tag stack and reconstruct it from the property's contents. Any edits made to [member text] will erase previous edits made from other manual sources such as [method append_text] and the [code]push_*[/code] / [method pop] methods.
[b]Note:[/b] RichTextLabel doesn't support entangled BBCode tags. For example, instead of using [code skip-lint][b]bold[i]bold italic[/b]italic[/i][/code], use [code skip-lint][b]bold[i]bold italic[/i][/b][i]italic[/i][/code].
[b]Note:[/b] [code]push_*/pop_*[/code] functions won't affect BBCode.
[b]Note:[/b] Unlike [Label], [RichTextLabel] doesn't have a [i]property[/i] to horizontally align text to the center. Instead, enable [member bbcode_enabled] and surround the text in a [code skip-lint][center][/code] tag as follows: [code skip-lint][center]Example[/center][/code]. There is currently no built-in way to vertically align text either, but this can be emulated by relying on anchors/containers and the [member fit_content] property.
$DOCS_URL/tutorials/ui/bbcode_in_richtextlabel.html
https://godotengine.org/asset-library/asset/2774
https://godotengine.org/asset-library/asset/2789
Adds an image's opening and closing tags to the tag stack, optionally providing a [param width] and [param height] to resize the image, a [param color] to tint the image and a [param region] to only use parts of the image.
If [param width] or [param height] is set to 0, the image size will be adjusted in order to keep the original aspect ratio.
If [param width] and [param height] are not set, but [param region] is, the region's rect will be used.
[param key] is an optional identifier, that can be used to modify the image via [method update_image].
If [param pad] is set, and the image is smaller than the size specified by [param width] and [param height], the image padding is added to match the size instead of upscaling.
If [param size_in_percent] is set, [param width] and [param height] values are percentages of the control width instead of pixels.
Adds raw non-BBCode-parsed text to the tag stack.
Parses [param bbcode] and adds tags to the tag stack as needed.
[b]Note:[/b] Using this method, you can't close a tag that was opened in a previous [method append_text] call. This is done to improve performance, especially when updating large RichTextLabels since rebuilding the whole BBCode every time would be slower. If you absolutely need to close a tag in a future method call, append the [member text] instead of using [method append_text].
Clears the tag stack, causing the label to display nothing.
[b]Note:[/b] This method does not affect [member text], and its contents will show again if the label is redrawn. However, setting [member text] to an empty [String] also clears the stack.
Clears the current selection.
Returns the line number of the character position provided. Line and character numbers are both zero-indexed.
[b]Note:[/b] If [member threaded] is enabled, this method returns a value for the loaded part of the document. Use [method is_finished] or [signal finished] to determine whether document is fully loaded.
Returns the paragraph number of the character position provided. Paragraph and character numbers are both zero-indexed.
[b]Note:[/b] If [member threaded] is enabled, this method returns a value for the loaded part of the document. Use [method is_finished] or [signal finished] to determine whether document is fully loaded.
Returns the height of the content.
[b]Note:[/b] If [member threaded] is enabled, this method returns a value for the loaded part of the document. Use [method is_finished] or [signal finished] to determine whether document is fully loaded.
Returns the width of the content.
[b]Note:[/b] If [member threaded] is enabled, this method returns a value for the loaded part of the document. Use [method is_finished] or [signal finished] to determine whether document is fully loaded.
Returns the total number of lines in the text. Wrapped text is counted as multiple lines.
[b]Note:[/b] If [member threaded] is enabled, this method returns a value for the loaded part of the document. Use [method is_finished] or [signal finished] to determine whether document is fully loaded.
Returns the vertical offset of the line found at the provided index.
[b]Note:[/b] If [member threaded] is enabled, this method returns a value for the loaded part of the document. Use [method is_finished] or [signal finished] to determine whether document is fully loaded.
Returns the [PopupMenu] of this [RichTextLabel]. By default, this menu is displayed when right-clicking on the [RichTextLabel].
You can add custom menu items or remove standard ones. Make sure your IDs don't conflict with the standard ones (see [enum MenuItems]). For example:
[codeblocks]
[gdscript]
func _ready():
var menu = get_menu()
# Remove "Select All" item.
menu.remove_item(MENU_SELECT_ALL)
# Add custom items.
menu.add_separator()
menu.add_item("Duplicate Text", MENU_MAX + 1)
# Connect callback.
menu.id_pressed.connect(_on_item_pressed)
func _on_item_pressed(id):
if id == MENU_MAX + 1:
add_text("\n" + get_parsed_text())
[/gdscript]
[csharp]
public override void _Ready()
{
var menu = GetMenu();
// Remove "Select All" item.
menu.RemoveItem(RichTextLabel.MenuItems.SelectAll);
// Add custom items.
menu.AddSeparator();
menu.AddItem("Duplicate Text", RichTextLabel.MenuItems.Max + 1);
// Add event handler.
menu.IdPressed += OnItemPressed;
}
public void OnItemPressed(int id)
{
if (id == TextEdit.MenuItems.Max + 1)
{
AddText("\n" + GetParsedText());
}
}
[/csharp]
[/codeblocks]
[b]Warning:[/b] This is a required internal node, removing and freeing it may cause a crash. If you wish to hide it or any of its children, use their [member Window.visible] property.
Returns the total number of paragraphs (newlines or [code]p[/code] tags in the tag stack's text tags). Considers wrapped text as one paragraph.
Returns the vertical offset of the paragraph found at the provided index.
[b]Note:[/b] If [member threaded] is enabled, this method returns a value for the loaded part of the document. Use [method is_finished] or [signal finished] to determine whether document is fully loaded.
Returns the text without BBCode mark-up.
Returns the current selection text. Does not include BBCodes.
Returns the current selection first character index if a selection is active, [code]-1[/code] otherwise. Does not include BBCodes.
Returns the current selection last character index if a selection is active, [code]-1[/code] otherwise. Does not include BBCodes.
Returns the total number of characters from text tags. Does not include BBCodes.
Returns the vertical scrollbar.
[b]Warning:[/b] This is a required internal node, removing and freeing it may cause a crash. If you wish to hide it or any of its children, use their [member CanvasItem.visible] property.
Returns the number of visible lines.
[b]Note:[/b] If [member threaded] is enabled, this method returns a value for the loaded part of the document. Use [method is_finished] or [signal finished] to determine whether document is fully loaded.
Returns the number of visible paragraphs. A paragraph is considered visible if at least one of its lines is visible.
[b]Note:[/b] If [member threaded] is enabled, this method returns a value for the loaded part of the document. Use [method is_finished] or [signal finished] to determine whether document is fully loaded.
Installs a custom effect. This can also be done in the Inspector through the [member custom_effects] property. [param effect] should be a valid [RichTextEffect].
[b]Example:[/b] With the following script extending from [RichTextEffect]:
[codeblock]
# effect.gd
class_name MyCustomEffect
extends RichTextEffect
var bbcode = "my_custom_effect"
# ...
[/codeblock]
The above effect can be installed in [RichTextLabel] from a script:
[codeblock]
# rich_text_label.gd
extends RichTextLabel
func _ready():
install_effect(MyCustomEffect.new())
# Alternatively, if not using `class_name` in the script that extends RichTextEffect:
install_effect(preload("res://effect.gd").new())
[/codeblock]
Invalidates [param paragraph] and all subsequent paragraphs cache.
If [member threaded] is enabled, returns [code]true[/code] if the background thread has finished text processing, otherwise always return [code]true[/code].
Returns whether the menu is visible. Use this instead of [code]get_menu().visible[/code] to improve performance (so the creation of the menu is avoided).
If [member threaded] is enabled, returns [code]true[/code] if the background thread has finished text processing, otherwise always return [code]true[/code].
Executes a given action as defined in the [enum MenuItems] enum.
Adds a newline tag to the tag stack.
The assignment version of [method append_text]. Clears the tag stack and inserts the new content.
Parses BBCode parameter [param expressions] into a dictionary.
Terminates the current tag. Use after [code]push_*[/code] methods to close BBCodes manually. Does not need to follow [code]add_*[/code] methods.
Terminates all tags opened by [code]push_*[/code] methods.
Terminates tags opened after the last [method push_context] call (including context marker), or all tags if there's no context marker on the stack.
Adds a [code skip-lint][bgcolor][/code] tag to the tag stack.
Adds a [code skip-lint][font][/code] tag with a bold font to the tag stack. This is the same as adding a [code skip-lint][b][/code] tag if not currently in a [code skip-lint][i][/code] tag.
Adds a [code skip-lint][font][/code] tag with a bold italics font to the tag stack.
Adds a [code skip-lint][cell][/code] tag to the tag stack. Must be inside a [code skip-lint][table][/code] tag. See [method push_table] for details. Use [method set_table_column_expand] to set column expansion ratio, [method set_cell_border_color] to set cell border, [method set_cell_row_background_color] to set cell background, [method set_cell_size_override] to override cell size, and [method set_cell_padding] to set padding.
Adds a [code skip-lint][color][/code] tag to the tag stack.
Adds a context marker to the tag stack. See [method pop_context].
Adds a custom effect tag to the tag stack. The effect does not need to be in [member custom_effects]. The environment is directly passed to the effect.
Adds a [code skip-lint][dropcap][/code] tag to the tag stack. Drop cap (dropped capital) is a decorative element at the beginning of a paragraph that is larger than the rest of the text.
Adds a [code skip-lint][fgcolor][/code] tag to the tag stack.
Adds a [code skip-lint][font][/code] tag to the tag stack. Overrides default fonts for its duration.
Passing [code]0[/code] to [param font_size] will use the existing default font size.
Adds a [code skip-lint][font_size][/code] tag to the tag stack. Overrides default font size for its duration.
Adds a [code skip-lint][hint][/code] tag to the tag stack. Same as BBCode [code skip-lint][hint=something]{text}[/hint][/code].
Adds an [code skip-lint][indent][/code] tag to the tag stack. Multiplies [param level] by current [member tab_size] to determine new margin length.
Adds a [code skip-lint][font][/code] tag with an italics font to the tag stack. This is the same as adding an [code skip-lint][i][/code] tag if not currently in a [code skip-lint][b][/code] tag.
Adds language code used for text shaping algorithm and Open-Type font features.
Adds [code skip-lint][ol][/code] or [code skip-lint][ul][/code] tag to the tag stack. Multiplies [param level] by current [member tab_size] to determine new margin length.
Adds a meta tag to the tag stack. Similar to the BBCode [code skip-lint][url=something]{text}[/url][/code], but supports non-[String] metadata types.
If [member meta_underlined] is [code]true[/code], meta tags display an underline. This behavior can be customized with [param underline_mode].
[b]Note:[/b] Meta tags do nothing by default when clicked. To assign behavior when clicked, connect [signal meta_clicked] to a function that is called when the meta tag is clicked.
Adds a [code skip-lint][font][/code] tag with a monospace font to the tag stack.
Adds a [code skip-lint][font][/code] tag with a normal font to the tag stack.
Adds a [code skip-lint][outline_color][/code] tag to the tag stack. Adds text outline for its duration.
Adds a [code skip-lint][outline_size][/code] tag to the tag stack. Overrides default text outline size for its duration.
Adds a [code skip-lint][p][/code] tag to the tag stack.
Adds a [code skip-lint][s][/code] tag to the tag stack.
Adds a [code skip-lint][table=columns,inline_align][/code] tag to the tag stack. Use [method set_table_column_expand] to set column expansion ratio. Use [method push_cell] to add cells.
Adds a [code skip-lint][u][/code] tag to the tag stack.
Removes a paragraph of content from the label. Returns [code]true[/code] if the paragraph exists.
The [param paragraph] argument is the index of the paragraph to remove, it can take values in the interval [code][0, get_paragraph_count() - 1][/code].
If [param no_invalidate] is set to [code]true[/code], cache for the subsequent paragraphs is not invalidated. Use it for faster updates if deleted paragraph is fully self-contained (have no unclosed tags), or this call is part of the complex edit operation and [method invalidate_paragraph] will be called at the end of operation.
Scrolls the window's top line to match [param line].
Scrolls the window's top line to match first line of the [param paragraph].
Scrolls to the beginning of the current selection.
Select all the text.
If [member selection_enabled] is [code]false[/code], no selection will occur.
Sets color of a table cell border.
Sets inner padding of a table cell.
Sets color of a table cell. Separate colors for alternating rows can be specified.
Sets minimum and maximum size overrides for a table cell.
Edits the selected column's expansion options. If [param expand] is [code]true[/code], the column expands in proportion to its expansion ratio versus the other columns' ratios.
For example, 2 columns with ratios 3 and 4 plus 70 pixels in available width would expand 30 and 40 pixels, respectively.
If [param expand] is [code]false[/code], the column will not contribute to the total ratio.
Updates the existing images with the key [param key]. Only properties specified by [param mask] bits are updated. See [method add_image].
If set to something other than [constant TextServer.AUTOWRAP_OFF], the text gets wrapped inside the node's bounding rectangle. To see how each mode behaves, see [enum TextServer.AutowrapMode].
If [code]true[/code], the label uses BBCode formatting.
[b]Note:[/b] This only affects the contents of [member text], not the tag stack.
If [code]true[/code], a right-click displays the context menu.
The currently installed custom effects. This is an array of [RichTextEffect]s.
To add a custom effect, it's more convenient to use [method install_effect].
If [code]true[/code], the selected text will be deselected when focus is lost.
If [code]true[/code], allow drag and drop of selected text.
If [code]true[/code], the label's minimum size will be automatically updated to fit its content, matching the behavior of [Label].
If [code]true[/code], the label underlines hint tags such as [code skip-lint][hint=description]{text}[/hint][/code].
Controls the text's horizontal alignment. Supports left, center, right, and fill, or justify. Set it to one of the [enum HorizontalAlignment] constants.
Line fill alignment rules. See [enum TextServer.JustificationFlag] for more information.
Language code used for line-breaking and text shaping algorithms, if left empty current locale is used instead.
If [code]true[/code], the label underlines meta tags such as [code skip-lint][url]{text}[/url][/code]. These tags can call a function when clicked if [signal meta_clicked] is connected to a function.
The delay after which the loading progress bar is displayed, in milliseconds. Set to [code]-1[/code] to disable progress bar entirely.
[b]Note:[/b] Progress bar is displayed only if [member threaded] is enabled.
If [code]true[/code], the scrollbar is visible. Setting this to [code]false[/code] does not block scrolling completely. See [method scroll_to_line].
If [code]true[/code], the window scrolls down to display new content automatically.
If [code]true[/code], the label allows text selection.
If [code]true[/code], shortcut keys for context menu items are enabled, even if the context menu is disabled.
Set BiDi algorithm override for the structured text.
Set additional options for BiDi override.
The number of spaces associated with a single tab length. Does not affect [code]\t[/code] in text tags, only indent tags.
Aligns text to the given tab-stops.
The label's text in BBCode format. Is not representative of manual modifications to the internal tag stack. Erases changes made by other methods when edited.
[b]Note:[/b] If [member bbcode_enabled] is [code]true[/code], it is unadvised to use the [code]+=[/code] operator with [member text] (e.g. [code]text += "some string"[/code]) as it replaces the whole text and can cause slowdowns. It will also erase all BBCode that was added to stack using [code]push_*[/code] methods. Use [method append_text] for adding text instead, unless you absolutely need to close a tag that was opened in an earlier method call.
Base text writing direction.
If [code]true[/code], text processing is done in a background thread.
Controls the text's vertical alignment. Supports top, center, bottom, and fill. Set it to one of the [enum VerticalAlignment] constants.
The number of characters to display. If set to [code]-1[/code], all characters are displayed. This can be useful when animating the text appearing in a dialog box.
[b]Note:[/b] Setting this property updates [member visible_ratio] accordingly.
Sets the clipping behavior when [member visible_characters] or [member visible_ratio] is set. See [enum TextServer.VisibleCharactersBehavior] for more info.
The fraction of characters to display, relative to the total number of characters (see [method get_total_character_count]). If set to [code]1.0[/code], all characters are displayed. If set to [code]0.5[/code], only half of the characters will be displayed. This can be useful when animating the text appearing in a dialog box.
[b]Note:[/b] Setting this property updates [member visible_characters] accordingly.
Triggered when the document is fully loaded.
Triggered when the user clicks on content between meta (URL) tags. If the meta is defined in BBCode, e.g. [code skip-lint][url={"key": "value"}]Text[/url][/code], then the parameter for this signal will always be a [String] type. If a particular type or an object is desired, the [method push_meta] method must be used to manually insert the data into the tag stack. Alternatively, you can convert the [String] input to the desired type based on its contents (such as calling [method JSON.parse] on it).
For example, the following method can be connected to [signal meta_clicked] to open clicked URLs using the user's default web browser:
[codeblocks]
[gdscript]
# This assumes RichTextLabel's `meta_clicked` signal was connected to
# the function below using the signal connection dialog.
func _richtextlabel_on_meta_clicked(meta):
# `meta` is of Variant type, so convert it to a String to avoid script errors at run-time.
OS.shell_open(str(meta))
[/gdscript]
[/codeblocks]
Triggers when the mouse exits a meta tag.
Triggers when the mouse enters a meta tag.
Each list item has a number marker.
Each list item has a letter marker.
Each list item has a roman number marker.
Each list item has a filled circle marker.
Copies the selected text.
Selects the whole [RichTextLabel] text.
Represents the size of the [enum MenuItems] enum.
Meta tag does not display an underline, even if [member meta_underlined] is [code]true[/code].
If [member meta_underlined] is [code]true[/code], meta tag always display an underline.
If [member meta_underlined] is [code]true[/code], meta tag display an underline when the mouse cursor is over it.
If this bit is set, [method update_image] changes image texture.
If this bit is set, [method update_image] changes image size.
If this bit is set, [method update_image] changes image color.
If this bit is set, [method update_image] changes image inline alignment.
If this bit is set, [method update_image] changes image texture region.
If this bit is set, [method update_image] changes image padding.
If this bit is set, [method update_image] changes image tooltip.
If this bit is set, [method update_image] changes image width from/to percents.
The default text color.
The default tint of text outline.
The color of selected text, used when [member selection_enabled] is [code]true[/code]. If equal to [code]Color(0, 0, 0, 0)[/code], it will be ignored.
The color of the font's shadow.
The color of the selection box.
The default cell border color.
The default background color for even rows.
The default background color for odd rows.
Additional vertical spacing between lines (in pixels), spacing is added to line descent. This value can be negative.
The size of the text outline.
[b]Note:[/b] If using a font with [member FontFile.multichannel_signed_distance_field] enabled, its [member FontFile.msdf_pixel_range] must be set to at least [i]twice[/i] the value of [theme_item outline_size] for outline rendering to look correct. Otherwise, the outline may appear to be cut off earlier than intended.
The horizontal offset of the font's shadow.
The vertical offset of the font's shadow.
The size of the shadow outline.
The horizontal separation of elements in a table.
The vertical separation of elements in a table.
The horizontal padding around boxes drawn by the [code][fgcolor][/code] and [code][bgcolor][/code] tags. This does not affect the appearance of text selection.
The vertical padding around boxes drawn by the [code][fgcolor][/code] and [code][bgcolor][/code] tags. This does not affect the appearance of text selection.
The font used for bold text.
The font used for bold italics text.
The font used for italics text.
The font used for monospace text.
The default text font.
The font size used for bold text.
The font size used for bold italics text.
The font size used for italics text.
The font size used for monospace text.
The default text font size.
The background used when the [RichTextLabel] is focused. The [theme_item focus] [StyleBox] is displayed [i]over[/i] the base [StyleBox], so a partially transparent [StyleBox] should be used to ensure the base [StyleBox] remains visible. A [StyleBox] that represents an outline or an underline works well for this purpose. To disable the focus visual effect, assign a [StyleBoxEmpty] resource. Note that disabling the focus visual effect will harm keyboard/controller navigation usability, so this is not recommended for accessibility reasons.
The normal background for the [RichTextLabel].