RichTextLabel
2026/4/14大约 3 分钟
最后同步日期:2026-04-15 | Godot 官方原文 — RichTextLabel
RichTextLabel
节点继承关系
继承链:Node → CanvasItem → Control → RichTextLabel
继承自 Control
| 类型 | 名称 | 说明 |
|---|---|---|
| 属性 | Size | 控件尺寸 |
| 属性 | Position | 控件位置 |
| 属性 | AnchorsPreset | 锚点预设 |
| 属性 | GrowDirection | 超出容器时的扩展方向 |
| 属性 | MouseFilter | 鼠标事件过滤(停止 / 通过 / 忽略) |
| 属性 | FocusMode | 焦点模式(无 / 单击 / 全部) |
| 信号 | gui_input | 接收到 GUI 输入事件 |
| 信号 | mouse_entered | 鼠标进入控件区域 |
| 信号 | mouse_exited | 鼠标离开控件区域 |
| 信号 | focus_entered | 获得焦点 |
| 信号 | focus_exited | 失去焦点 |
| 信号 | resized | 尺寸变化 |
| 方法 | GrabFocus() | 获取焦点 |
| 方法 | ReleaseFocus() | 释放焦点 |
| 方法 | SetAnchorsPreset() | 设置锚点预设 |
| 方法 | GetMinimumSize() | 获取最小尺寸 |
继承自 CanvasItem
| 类型 | 名称 | 说明 |
|---|---|---|
| 属性 | Visible | 是否可见 |
| 属性 | Modulate | 整体颜色叠加(乘法) |
| 属性 | SelfModulate | 自身颜色叠加(不影响子节点) |
| 属性 | ZIndex | 绘制层级(Z 轴排序) |
| 信号 | visibility_changed | 可见性变化时触发 |
| 方法 | GetGlobalMousePosition() | 获取鼠标全局坐标 |
继承自 Node
| 类型 | 名称 | 说明 |
|---|---|---|
| 属性 | Name | 节点名称 |
| 属性 | ProcessMode | 处理模式(始终 / 暂停时 / 仅编辑器) |
| 属性 | ProcessPriority | 处理优先级,数字越小越先执行 |
| 信号 | ready | 节点进入场景树并准备就绪 |
| 信号 | tree_entered | 节点进入场景树 |
| 信号 | tree_exited | 节点完全离开场景树 |
| 方法 | GetNode<T>() | 按路径获取子节点 |
| 方法 | AddChild() | 添加子节点 |
| 方法 | RemoveChild() | 移除子节点 |
| 方法 | QueueFree() | 帧结束后释放节点 |
| 方法 | GetParent() | 获取父节点 |
定义
RichTextLabel 可以显示带格式的文字——加粗、斜体、不同颜色、甚至图片。就像微信聊天里你能发带格式的消息一样。它使用一种叫 BBCode 的标记语法——就是用方括号来给文字加格式,比如 [b]加粗[/b]。
使用频率:★★★ 一般常用(需要格式化文字时使用)
节点用途
- 显示带格式、带颜色的文字
- 游戏说明/帮助文档
- 对话框中的富文本对话
- 嵌入图片和超链接的文本
使用场景
- 游戏开场的故事叙述
- 角色对话(不同角色不同颜色)
- 游戏内帮助文档
- 任务描述(带图标和颜色标记)
常用节点搭配
- 搭配
ScrollContainer显示长文本 - 搭配
MarginContainer加边距
生效必备素材/资源
无需特殊资源。可选:BBCode 中引用的图片资源。
节点属性与信号
自有属性
| 属性 | 类型 | 默认值 | 继承自 | 说明 |
|---|---|---|---|---|
bbcode_enabled | bool | false | — | 是否启用 BBCode 解析 |
text | string | "" | — | 带格式的文字内容(BBCode 语法) |
fit_content | bool | true | — | 是否自动适配内容高度 |
scroll_active | bool | true | — | 是否启用滚动 |
visible_characters | int | -1 | — | 可见字符数(用于打字机效果) |
常用 BBCode 标签
| 标签 | 效果 | 示例 |
|---|---|---|
[b]...[/b] | 加粗 | [b]重要[/b] |
[i]...[/i] | 斜体 | [i]提示[/i] |
[color=red]...[/color] | 改颜色 | [color=red]警告[/color] |
[img]路径[/img] | 插入图片 | [img]res://icon.svg[/img] |
[url]...[/url] | 超链接 | [url]https://godotengine.org[/url] |
[center]...[/center] | 居中 | [center]标题[/center] |
[table=2][tr][td]...[/td][/tr][/table] | 表格 | 多列布局 |
信号
| 信号 | 触发时机 | 参数 |
|---|---|---|
meta_clicked | 点击超链接或元标签时 | Variant 元数据 |
finished | 打字机效果播放完毕时 | 无 |
常用方法
| 方法 | 说明 |
|---|---|
AppendText(string) | 追加文本 |
Clear() | 清空内容 |
SetVisibleCharacters(int) | 设置可见字符数(打字机效果) |
代码示例
C
using Godot;
public partial class RichTextDemo : Control
{
public override void _Ready()
{
var richLabel = new RichTextLabel();
richLabel.BbcodeEnabled = true;
richLabel.Text = "[b]游戏说明[/b]\n" +
"[color=green]使用 WASD 移动[/color]\n" +
"[color=yellow]空格键跳跃[/color]\n" +
"[i]祝你好运![/i]";
richLabel.Position = new Vector2(50, 50);
richLabel.Size = new Vector2(300, 150);
AddChild(richLabel);
}
}GDScript
extends Control
func _ready():
var rich_label = RichTextLabel.new()
rich_label.bbcode_enabled = true
rich_label.text = "[b]游戏说明[/b]\n" + \
"[color=green]使用 WASD 移动[/color]\n" + \
"[color=yellow]空格键跳跃[/color]\n" + \
"[i]祝你好运![/i]"
rich_label.position = Vector2(50, 50)
rich_label.size = Vector2(300, 150)
add_child(rich_label)