Up to date
This page is up to date for Godot 4.2
.
If you still find outdated information, please open an issue.
TextEdit¶
继承: Control < CanvasItem < Node < Object
派生: CodeEdit
多行文本编辑器。
描述¶
多行文本编辑器。它还有少量用于编辑代码的功能,例如语法高亮支持。更多针对编辑代码的高阶功能见 CodeEdit。
注意:大多数视口、光标和编辑方法都包含 caret_index
参数以支持 caret_multiple。该参数应为以下之一:-1
用于所有光标,0
用于主光标,大于 0
用于辅助光标。
注意:当按住 Alt 时,垂直滚轮的滚动速度将是正常速度的 5 倍。这也适用于 Godot 脚本编辑器。
属性¶
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
focus_mode |
|
|
|
||
|
||
|
||
|
||
|
||
|
||
mouse_default_cursor_shape |
|
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
方法¶
void |
_backspace ( int caret_index ) virtual |
void |
|
void |
|
void |
_handle_unicode_input ( int unicode_char, int caret_index ) virtual |
void |
|
void |
_paste_primary_clipboard ( int caret_index ) virtual |
void |
add_caret_at_carets ( bool below ) |
void |
add_gutter ( int at=-1 ) |
void |
|
void |
adjust_carets_after_edit ( int caret, int from_line, int from_col, int to_line, int to_col ) |
void |
adjust_viewport_to_caret ( int caret_index=0 ) |
void |
|
void |
|
void |
center_viewport_to_caret ( int caret_index=0 ) |
void |
clear ( ) |
void |
|
void |
|
void |
|
void |
delete_selection ( int caret_index=-1 ) |
void |
|
void |
end_action ( ) |
void |
|
get_caret_column ( int caret_index=0 ) const |
|
get_caret_count ( ) const |
|
get_caret_draw_pos ( int caret_index=0 ) const |
|
get_caret_line ( int caret_index=0 ) const |
|
get_caret_wrap_index ( int caret_index=0 ) const |
|
get_first_non_whitespace_column ( int line ) const |
|
get_first_visible_line ( ) const |
|
get_gutter_count ( ) const |
|
get_gutter_name ( int gutter ) const |
|
get_gutter_type ( int gutter ) const |
|
get_gutter_width ( int gutter ) const |
|
get_h_scroll_bar ( ) const |
|
get_indent_level ( int line ) const |
|
get_last_full_visible_line ( ) const |
|
get_last_full_visible_line_wrap_index ( ) const |
|
get_last_unhidden_line ( ) const |
|
get_line_background_color ( int line ) const |
|
get_line_column_at_pos ( Vector2i position, bool allow_out_of_bounds=true ) const |
|
get_line_count ( ) const |
|
get_line_gutter_icon ( int line, int gutter ) const |
|
get_line_gutter_item_color ( int line, int gutter ) const |
|
get_line_gutter_metadata ( int line, int gutter ) const |
|
get_line_gutter_text ( int line, int gutter ) const |
|
get_line_height ( ) const |
|
get_line_width ( int line, int wrap_index=-1 ) const |
|
get_line_wrap_count ( int line ) const |
|
get_line_wrap_index_at_column ( int line, int column ) const |
|
get_line_wrapped_text ( int line ) const |
|
get_local_mouse_pos ( ) const |
|
get_menu ( ) const |
|
get_minimap_line_at_pos ( Vector2i position ) const |
|
get_minimap_visible_lines ( ) const |
|
get_next_visible_line_index_offset_from ( int line, int wrap_index, int visible_amount ) const |
|
get_next_visible_line_offset_from ( int line, int visible_amount ) const |
|
get_pos_at_line_column ( int line, int column ) const |
|
get_rect_at_line_column ( int line, int column ) const |
|
get_saved_version ( ) const |
|
get_scroll_pos_for_line ( int line, int wrap_index=0 ) const |
|
get_selected_text ( int caret_index=-1 ) |
|
get_selection_column ( int caret_index=0 ) const |
|
get_selection_from_column ( int caret_index=0 ) const |
|
get_selection_from_line ( int caret_index=0 ) const |
|
get_selection_line ( int caret_index=0 ) const |
|
get_selection_mode ( ) const |
|
get_selection_to_column ( int caret_index=0 ) const |
|
get_selection_to_line ( int caret_index=0 ) const |
|
get_tab_size ( ) const |
|
get_total_gutter_width ( ) const |
|
get_total_visible_line_count ( ) const |
|
get_v_scroll_bar ( ) const |
|
get_version ( ) const |
|
get_visible_line_count ( ) const |
|
get_visible_line_count_in_range ( int from_line, int to_line ) const |
|
get_word_at_pos ( Vector2 position ) const |
|
get_word_under_caret ( int caret_index=-1 ) const |
|
has_ime_text ( ) const |
|
has_redo ( ) const |
|
has_selection ( int caret_index=-1 ) const |
|
has_undo ( ) const |
|
void |
insert_line_at ( int line, String text ) |
void |
insert_text_at_caret ( String text, int caret_index=-1 ) |
is_caret_visible ( int caret_index=0 ) const |
|
is_dragging_cursor ( ) const |
|
is_gutter_clickable ( int gutter ) const |
|
is_gutter_drawn ( int gutter ) const |
|
is_gutter_overwritable ( int gutter ) const |
|
is_line_gutter_clickable ( int line, int gutter ) const |
|
is_line_wrapped ( int line ) const |
|
is_menu_visible ( ) const |
|
is_mouse_over_selection ( bool edges, int caret_index=-1 ) const |
|
is_overtype_mode_enabled ( ) const |
|
void |
menu_option ( int option ) |
void |
merge_gutters ( int from_line, int to_line ) |
void |
|
void |
|
void |
paste_primary_clipboard ( int caret_index=-1 ) |
void |
redo ( ) |
void |
remove_caret ( int caret ) |
void |
remove_gutter ( int gutter ) |
void |
|
void |
remove_text ( int from_line, int from_column, int to_line, int to_column ) |
search ( String text, int flags, int from_line, int from_colum ) const |
|
void |
select ( int from_line, int from_column, int to_line, int to_column, int caret_index=0 ) |
void |
select_all ( ) |
void |
select_word_under_caret ( int caret_index=-1 ) |
void |
set_caret_column ( int column, bool adjust_viewport=true, int caret_index=0 ) |
void |
set_caret_line ( int line, bool adjust_viewport=true, bool can_be_hidden=true, int wrap_index=0, int caret_index=0 ) |
void |
set_gutter_clickable ( int gutter, bool clickable ) |
void |
set_gutter_custom_draw ( int column, Callable draw_callback ) |
void |
set_gutter_draw ( int gutter, bool draw ) |
void |
set_gutter_name ( int gutter, String name ) |
void |
set_gutter_overwritable ( int gutter, bool overwritable ) |
void |
set_gutter_type ( int gutter, GutterType type ) |
void |
set_gutter_width ( int gutter, int width ) |
void |
|
void |
set_line_as_center_visible ( int line, int wrap_index=0 ) |
void |
set_line_as_first_visible ( int line, int wrap_index=0 ) |
void |
set_line_as_last_visible ( int line, int wrap_index=0 ) |
void |
set_line_background_color ( int line, Color color ) |
void |
set_line_gutter_clickable ( int line, int gutter, bool clickable ) |
void |
set_line_gutter_icon ( int line, int gutter, Texture2D icon ) |
void |
set_line_gutter_item_color ( int line, int gutter, Color color ) |
void |
set_line_gutter_metadata ( int line, int gutter, Variant metadata ) |
void |
set_line_gutter_text ( int line, int gutter, String text ) |
void |
set_overtype_mode_enabled ( bool enabled ) |
void |
set_search_flags ( int flags ) |
void |
set_search_text ( String search_text ) |
void |
set_selection_mode ( SelectionMode mode, int line=-1, int column=-1, int caret_index=0 ) |
void |
set_tab_size ( int size ) |
void |
set_tooltip_request_func ( Callable callback ) |
void |
start_action ( EditAction action ) |
void |
swap_lines ( int from_line, int to_line ) |
void |
|
void |
undo ( ) |
主题属性¶
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
信号¶
caret_changed ( )
光标改变位置时发出。
gutter_added ( )
添加边栏时发出。
gutter_clicked ( int line, int gutter )
点击边栏时发出。
gutter_removed ( )
移除边栏时发出。
lines_edited_from ( int from_line, int to_line )
文本改变时立即发出。
添加文本时 from_line
小于 to_line
。移除文本时 to_line
小于 from_line
。
text_changed ( )
当文本更改时触发。
text_set ( )
枚举¶
剪切(复制并删除)选中的文本。
复制选中的文本。
将剪贴板上的文本粘贴在选定的文本上(或光标的位置)。
擦除整个 TextEdit 文本。
选择整个 TextEdit 文本。
撤销之前的操作。
重做前一个动作。
“文本书写方向”子菜单的 ID。
将文本方向设置为继承。
将文本方向设置为自动。
将文本方向设置为从左至右。
将文本方向设置为从右至左。
切换控制字符的显示。
“插入控制字符”子菜单的 ID。
插入从左至右标志(LRM)字符。
插入从右至左标志(LRM)字符。
插入开始从左至右嵌入(LRE)字符。
插入开始从右至左嵌入(RLE)字符。
插入开始从左至右强制(LRO)字符。
插入开始从右至左嵌入(RLE)字符。
插入退出方向格式化(PDF)字符。
插入阿拉伯字母标记(ALM)字符。
插入从左至右隔离(LRI)字符。
插入从右至左隔离(RLI)字符。
插入第一个强隔离(FSI)字符。
插入退出方向隔离(PDI)字符。
插入零宽连接符(ZWJ)字符。
插入零宽非连接符(ZWNJ)字符。
插入文字连接符(WJ)字符。
插入软连字符(SHY)字符。
代表 MenuItems 枚举的大小。
enum EditAction:
EditAction ACTION_NONE = 0
无当前动作。
EditAction ACTION_TYPING = 1
打字动作。
EditAction ACTION_BACKSPACE = 2
向后删除动作。
EditAction ACTION_DELETE = 3
向前删除动作。
enum SearchFlags:
SearchFlags SEARCH_MATCH_CASE = 1
搜索时匹配大小写。
SearchFlags SEARCH_WHOLE_WORDS = 2
搜索时匹配整个单词。
SearchFlags SEARCH_BACKWARDS = 4
从头到尾搜索。
enum CaretType:
CaretType CARET_TYPE_LINE = 0
垂直线光标。
CaretType CARET_TYPE_BLOCK = 1
方块光标。
enum SelectionMode:
SelectionMode SELECTION_MODE_NONE = 0
不选择。
SelectionMode SELECTION_MODE_SHIFT = 1
就像按下 shift
一样进行选择。
SelectionMode SELECTION_MODE_POINTER = 2
选择单个字符,就像用户单击一样。
SelectionMode SELECTION_MODE_WORD = 3
选择整个单词,就像用户双击一样。
SelectionMode SELECTION_MODE_LINE = 4
选择整行文本,就像用户三击一样。
enum LineWrappingMode:
LineWrappingMode LINE_WRAPPING_NONE = 0
换行被禁用。
LineWrappingMode LINE_WRAPPING_BOUNDARY = 1
换行发生在控件边界,超出通常可见的范围。
enum GutterType:
GutterType GUTTER_TYPE_STRING = 0
绘制字符串。
GutterType GUTTER_TYPE_ICON = 1
绘制图标。
GutterType GUTTER_TYPE_CUSTOM = 2
自定义绘制。
属性说明¶
AutowrapMode autowrap_mode = 3
void set_autowrap_mode ( AutowrapMode value )
AutowrapMode get_autowrap_mode ( )
如果 wrap_mode 设置为 LINE_WRAPPING_BOUNDARY,则设置文本换行模式。不同模式的行为见 AutowrapMode。
bool caret_blink = false
如果为 true
,则文本光标会闪烁。
float caret_blink_interval = 0.65
文本光标的闪烁间隔(单位为秒)。
bool caret_draw_when_editable_disabled = false
void set_draw_caret_when_editable_disabled ( bool value )
bool is_drawing_caret_when_editable_disabled ( )
如果为 true
,则禁用 editable 时光标可见。
bool caret_mid_grapheme = false
允许在单个复合字符的组件中进行移动光标、选中、删除的操作。
注意:退格键 Backspace 始终按复合字符的组件删除。
bool caret_move_on_right_click = true
void set_move_caret_on_right_click_enabled ( bool value )
bool is_move_caret_on_right_click_enabled ( )
如果为 true
,则单击右键时会先将文本光标移动到鼠标位置,然后再显示上下文菜单。
如果为 false
,则上下文菜单将忽略鼠标位置。
bool caret_multiple = true
设置是否允许使用多个文本光标。
CaretType caret_type = 0
设置要绘制的文本光标的类型。
为 true
时右键单击会显示上下文菜单。
bool deselect_on_focus_loss_enabled = true
如果为 true
,则在丢失焦点时会取消选中文本。
bool drag_and_drop_selection_enabled = true
如果为 true
,则允许拖放选中的文本。
bool draw_control_chars = false
如果为 true
,则会显示控制字符。
bool draw_spaces = false
如果为 true
,“空格”字符将具有可见表示形式。
bool draw_tabs = false
如果为 true
,“制表符”字符将具有可见表示形式。
bool editable = true
如果为 false
,则不能修改现在的文本,也不能添加新文本。
bool highlight_all_occurrences = false
如果为 true
,所选文本的所有匹配项都将突出显示。
bool highlight_current_line = false
如果为 true
,则包含光标的行会突出显示。
String language = ""
语言代码,用于断行和文本塑形算法,如果留空则使用当前区域设置。
bool middle_mouse_paste_enabled = true
如果为 false
,将禁用鼠标中键粘贴剪贴板。
注意:这个方法只在 Linux 上实现。
bool minimap_draw = false
如果为 true
,则显示小地图,提供源代码的概要。
int minimap_width = 80
小地图的宽度(以像素为单位)。
String placeholder_text = ""
TextEdit 为空时显示的文本。它不是 TextEdit 的默认值(参见 text)。
bool scroll_fit_content_height = false
如果为 true
,则 TextEdit 将禁用垂直滚动,并将最小高度与可见行数相匹配。
int scroll_horizontal = 0
如果有一个水平滚动条,这决定了当前的水平滚动值,单位是像素。
bool scroll_past_end_of_file = false
允许滚动过最后一行,进入“虚拟”空间。
bool scroll_smooth = false
在文本上平滑滚动,而不是跳到下一个位置。
float scroll_v_scroll_speed = 80.0
设置使用小地图或当 scroll_smooth 被启用时的滚动速度。
float scroll_vertical = 0.0
如果有一个垂直滚动条,这就决定了当前的垂直滚动值,以行数为单位,顶行从0开始。
bool selecting_enabled = true
如果为 true
,文本可以被选择。
如果为 false
,用户或使用select或select_all方法都不能选择文本。
bool shortcut_keys_enabled = true
为 true
时,即使上下文菜单已被禁用,也会启用该上下文菜单的快捷键。
StructuredTextParser structured_text_bidi_override = 0
void set_structured_text_bidi_override ( StructuredTextParser value )
StructuredTextParser get_structured_text_bidi_override ( )
为结构化文本设置 BiDi 算法覆盖。
Array structured_text_bidi_override_options = []
void set_structured_text_bidi_override_options ( Array value )
Array get_structured_text_bidi_override_options ( )
设置 BiDi 覆盖的附加选项。
SyntaxHighlighter syntax_highlighter
void set_syntax_highlighter ( SyntaxHighlighter value )
SyntaxHighlighter get_syntax_highlighter ( )
设置要使用的 SyntaxHighlighter。
String text = ""
TextEdit 的字符串值。
TextDirection text_direction = 0
void set_text_direction ( TextDirection value )
TextDirection get_text_direction ( )
基础文本书写方向。
bool virtual_keyboard_enabled = true
如果为 true
,则在获得焦点时会在支持的平台上显示原生虚拟键盘。
LineWrappingMode wrap_mode = 0
void set_line_wrapping_mode ( LineWrappingMode value )
LineWrappingMode get_line_wrapping_mode ( )
设置要使用的换行模式。
方法说明¶
void _backspace ( int caret_index ) virtual
覆盖此方法可以定义用户按下退格键时应该发生什么。
void _copy ( int caret_index ) virtual
覆盖此方法可以定义用户执行复制操作时应该发生什么。
void _cut ( int caret_index ) virtual
覆盖此方法可以定义用户执行剪切操作时应该发生什么。
void _handle_unicode_input ( int unicode_char, int caret_index ) virtual
覆盖此方法可以定义用户打出所提供的键 unicode_char
时应该发生什么。
void _paste ( int caret_index ) virtual
覆盖此方法可以定义用户执行粘贴操作时应该发生什么。
void _paste_primary_clipboard ( int caret_index ) virtual
覆盖此方法可以定义用户使用鼠标中键执行粘贴操作时应该发生什么。
注意:此方法仅在 Linux 上实现。
int add_caret ( int line, int col )
在给定的位置添加新的光标。返回新光标的索引,如果位置无效则返回 -1
。
void add_caret_at_carets ( bool below )
在每个光标上方或下方添加一个额外的光标。如果 below
为 true,则会在下方添加新光标,否则为上方。
void add_gutter ( int at=-1 )
为这个 TextEdit 注册一个新的边栏。请使用 at
指定边栏的顺序。-1
会将该边栏追加到右侧。
void add_selection_for_next_occurrence ( )
选中当前所选内容下一次出现的位置并添加文本光标。如果没有活动的选中内容,则选中当前光标所处的单词。
void adjust_carets_after_edit ( int caret, int from_line, int from_col, int to_line, int to_col )
重新定位受编辑影响的文本光标。这个操作假定编辑是按照编辑顺序应用的,见 get_caret_index_edit_order。
void adjust_viewport_to_caret ( int caret_index=0 )
调整视口,让光标可见。
void backspace ( int caret_index=-1 )
用户按下退格键时调用。可以用 _backspace 覆盖。
void begin_complex_operation ( )
开始一个多部分编辑。在调用 end_complex_operation 之前,所有编辑都将被视为一个动作。
void center_viewport_to_caret ( int caret_index=0 )
将视口居中在正在编辑的插入符号所在的行上。这也将 scroll_horizontal 值重置为 0
。
void clear ( )
执行对 TextEdit 的完全重置,包括撤消历史。
void clear_undo_history ( )
清除撤销历史。
void copy ( int caret_index=-1 )
复制当前选区。可以使用 _copy 覆盖。
void cut ( int caret_index=-1 )
剪切当前选区。可以使用 _cut 覆盖。
void delete_selection ( int caret_index=-1 )
删除选定的文本。
void deselect ( int caret_index=-1 )
取消当前选择。
void end_action ( )
标记在 start_action 开始的当前动作中步骤的末尾。
void end_complex_operation ( )
结束一个多部分编辑,该多部分编辑从 begin_complex_operation 开始。如果在一个复杂动作之外调用,则将当前动作将被压入撤消/重做堆栈。
int get_caret_column ( int caret_index=0 ) const
返回编辑光标所在的列。
int get_caret_count ( ) const
返回该 TextEdit 中的光标数。
Vector2 get_caret_draw_pos ( int caret_index=0 ) const
返回光标的像素绘制位置。
PackedInt32Array get_caret_index_edit_order ( )
返回文本光标索引的列表,使用编辑顺序,从下到上进行。编辑顺序是 insert_text_at_caret 等动作的应用顺序。
int get_caret_line ( int caret_index=0 ) const
返回编辑光标所在的行。
int get_caret_wrap_index ( int caret_index=0 ) const
返回正编辑的文本光标所在的换行索引。
int get_first_non_whitespace_column ( int line ) const
返回第一个非空白字符列。
int get_first_visible_line ( ) const
返回第一个可见行。
int get_gutter_count ( ) const
返回注册的边栏数量。
String get_gutter_name ( int gutter ) const
返回给定索引处边栏的名称。
GutterType get_gutter_type ( int gutter ) const
返回给定索引处边栏的类型。
int get_gutter_width ( int gutter ) const
返回给定索引处边栏的宽度。
HScrollBar get_h_scroll_bar ( ) const
设置该 TextEdit 所使用的 HScrollBar。
int get_indent_level ( int line ) const
返回第一个字符前的空格和 tab * tab_size
数。
int get_last_full_visible_line ( ) const
返回最后一个可见的行。获得换行索引的话,使用 get_last_full_visible_line_wrap_index。
int get_last_full_visible_line_wrap_index ( ) const
返回最后一个可见行的最后一个可见换行索引。
返回整个 TextEdit 中最后一个未隐藏的行。
String get_line ( int line ) const
返回特定行的文本。
Color get_line_background_color ( int line ) const
返回该行的当前背景色。如果没有设置颜色,则返回 Color(0, 0, 0, 0)
。
Vector2i get_line_column_at_pos ( Vector2i position, bool allow_out_of_bounds=true ) const
返回给定位置的行和列。在返回的向量中,x
是列,y
是行。如果 allow_out_of_bounds
为 false
,且该位置不在文本上方,则两个向量值都将被设置为 -1
。
int get_line_count ( ) const
返回文本中的行数。
Texture2D get_line_gutter_icon ( int line, int gutter ) const
返回边栏 gutter
中,当前位于 line
行的图标。
Color get_line_gutter_item_color ( int line, int gutter ) const
返回边栏 gutter
中,当前位于 line
行的颜色。
Variant get_line_gutter_metadata ( int line, int gutter ) const
返回边栏 gutter
中,当前位于 line
行的元数据。
String get_line_gutter_text ( int line, int gutter ) const
返回边栏 gutter
中,当前位于 line
行的文本。
int get_line_height ( ) const
返回所有行中行高的最大值。
注意:返回值受 line_spacing 和 font_size 的影响。不会比 1
小。
int get_line_width ( int line, int wrap_index=-1 ) const
返回位于 line
的 wrap_index
的像素宽度。
int get_line_wrap_count ( int line ) const
返回给定行换行的次数。
int get_line_wrap_index_at_column ( int line, int column ) const
返回给定行的列的换行索引。
PackedStringArray get_line_wrapped_text ( int line ) const
返回代表各个换行索引的 String 数组。
Vector2 get_local_mouse_pos ( ) const
返回针对文本方向调整过的局部鼠标位置。
返回该 TextEdit 的 PopupMenu。默认情况下,会在右键单击 TextEdit 时显示改菜单。
你可以添加自定义菜单项,也可以移除标准菜单项。请确保你使用的 ID 与标准菜单项的 ID 不冲突(见 MenuItems)。例如:
func _ready():
var menu = get_menu()
# 移除“撤销”后的所有菜单项。
menu.item_count = menu.get_item_index(MENU_REDO) + 1
# 添加自定义菜单项。
menu.add_separator()
menu.add_item("插入日期", MENU_MAX + 1)
# 连接回调。
menu.id_pressed.connect(_on_item_pressed)
func _on_item_pressed(id):
if id == MENU_MAX + 1:
insert_text_at_caret(Time.get_date_string_from_system())
public override void _Ready()
{
var menu = GetMenu();
// 移除“撤销”后的所有菜单项。
menu.ItemCount = menu.GetItemIndex(TextEdit.MenuItems.Redo) + 1;
// 添加自定义菜单项。
menu.AddSeparator();
menu.AddItem("插入日期", TextEdit.MenuItems.Max + 1);
// 添加事件处理器。
menu.IdPressed += OnItemPressed;
}
public void OnItemPressed(int id)
{
if (id == TextEdit.MenuItems.Max + 1)
{
InsertTextAtCaret(Time.GetDateStringFromSystem());
}
}
警告:这是必要的内部节点,将其移除或释放会导致崩溃。如果你想要将其隐藏,或者将其子节点隐藏,请使用对应的 Window.visible 属性。
int get_minimap_line_at_pos ( Vector2i position ) const
返回小地图 position
处等价的行。
int get_minimap_visible_lines ( ) const
返回小地图上能够绘制的行数。
Vector2i get_next_visible_line_index_offset_from ( int line, int wrap_index, int visible_amount ) const
类似于 get_next_visible_line_offset_from,但考虑了换行索引。在返回的向量中,x
是行,y
是换行索引。
int get_next_visible_line_offset_from ( int line, int visible_amount ) const
返回从 line
到 line + visible_amount
的下一个可见行的计数。也可以倒数计数。例如,如果 TextEdit 有 5 行,其中隐藏了第 2 行和第 3 行,使用 line = 1, visible_amount = 1
调用它会返回 3。
Vector2i get_pos_at_line_column ( int line, int column ) const
返回给定 line
和 column
的局部位置。如果返回的向量的 x
或 y
等于 -1
,则该位置在该控件的可视区域之外。
注意:Y 位置对应于行的底部。请使用 get_rect_at_line_column 来获取顶部位置。
Rect2i get_rect_at_line_column ( int line, int column ) const
返回给定 line
和 column
处的字素的局部位置和大小。如果返回的矩形的 x
或 y
位置等于 -1
,则该位置在该控件的可视区域之外。
注意:返回的矩形的 Y 位置对应于该行的顶部,不像 get_pos_at_line_column 返回底边。
int get_saved_version ( ) const
从 tag_saved_version 返回最后一个标记的保存版本。
float get_scroll_pos_for_line ( int line, int wrap_index=0 ) const
返回 line
的 wrap_index
对应的滚动位置。
String get_selected_text ( int caret_index=-1 )
返回光标所选文本,如果 caret_index
为默认值 -1
则为所有光标所选文本。
int get_selection_column ( int caret_index=0 ) const
返回选区的原始起始列。
int get_selection_from_column ( int caret_index=0 ) const
返回选择的开始列。
int get_selection_from_line ( int caret_index=0 ) const
返回选择开始行。
int get_selection_line ( int caret_index=0 ) const
返回选区的原始起始行。
SelectionMode get_selection_mode ( ) const
返回当前的选区模式。
int get_selection_to_column ( int caret_index=0 ) const
返回选择结束列。
int get_selection_to_line ( int caret_index=0 ) const
返回选择结束行。
int get_tab_size ( ) const
返回该 TextEdit 的制表符大小。
int get_total_gutter_width ( ) const
返回所有栏位及内部边距的总宽度。
int get_total_visible_line_count ( ) const
返回可绘制的行数。
VScrollBar get_v_scroll_bar ( ) const
设置该 TextEdit 所使用的 StyleBox。
int get_version ( ) const
返回该 TextEdit 的当前版本。版本是撤消/重做历史所记录操作的计数。
int get_visible_line_count ( ) const
返回可见行数,包括自动换行。
int get_visible_line_count_in_range ( int from_line, int to_line ) const
返回两行之间,可见行与换行的行的总数。
String get_word_at_pos ( Vector2 position ) const
返回位于 position
的单词。
String get_word_under_caret ( int caret_index=-1 ) const
返回一个 String 文本,其中包含文本光标位置下的单词。
bool has_ime_text ( ) const
返回用户是否有 IME 文本。
bool has_redo ( ) const
有“重做”动作可用时返回 true
。
bool has_selection ( int caret_index=-1 ) const
如果用户选中了文本,则返回 true
。
bool has_undo ( ) const
有“撤销”动作可用时返回 true
。
void insert_line_at ( int line, String text )
在第 line
行插入文本为 text
的新行。
void insert_text_at_caret ( String text, int caret_index=-1 )
在光标位置插入指定的文本。
bool is_caret_visible ( int caret_index=0 ) const
如果光标在屏幕上可见,则返回 true
。
bool is_dragging_cursor ( ) const
如果用户拖动鼠标进行滚动或选择,则返回 true
。
bool is_gutter_clickable ( int gutter ) const
返回该边栏是否可点击。
bool is_gutter_drawn ( int gutter ) const
返回该边栏是否正被绘制。
bool is_gutter_overwritable ( int gutter ) const
返回该边栏是否可覆写。
bool is_line_gutter_clickable ( int line, int gutter ) const
返回该边栏的给定行是否可点击。
bool is_line_wrapped ( int line ) const
返回给定的行是否换行。
返回菜单是否可见。请使用这个方法来代替 get_menu().visible
,可以提高性能(因为避免了菜单的创建)。
bool is_mouse_over_selection ( bool edges, int caret_index=-1 ) const
返回鼠标是否位于选区之上。如果 edges
为 true
,则边界会被认为是选区的一部分。
bool is_overtype_mode_enabled ( ) const
返回用户是否处于改写模式。
执行 MenuItems 枚举中定义的给定操作。
void merge_gutters ( int from_line, int to_line )
合并从 from_line
到 to_line
的边栏。只会复制可覆盖的边栏。
void merge_overlapping_carets ( )
合并重叠的文本光标。会保留最新的光标,或者存在选中内容的光标。
注意:光标改变位置后不会进行调用,而是在某些动作之后调用,所以进入光标重叠的状态是可能的。
void paste ( int caret_index=-1 )
粘贴到当前位置。可以用 _paste 覆盖。
void paste_primary_clipboard ( int caret_index=-1 )
粘贴主剪贴板。
void redo ( )
执行重做操作。
void remove_caret ( int caret )
移除给定的文本光标索引。
注意:可能导致所有其他光标索引的调整。
void remove_gutter ( int gutter )
从 TextEdit 中移除该边栏。
void remove_secondary_carets ( )
移除所有额外的光标。
void remove_text ( int from_line, int from_column, int to_line, int to_column )
移除给定位置之间的文本。
注意:文本光标和选区不会进行调整,因此可能最终处于无效位置。
Vector2i search ( String text, int flags, int from_line, int from_colum ) const
在文本内部进行搜索。搜索标志可以在 SearchFlags 枚举中指定。
在返回的向量中,x
是列,y
是行。如果没有找到结果,则两者都等于 -1
。
var result = search("print", SEARCH_WHOLE_WORDS, 0, 0)
if result.x != -1:
# 找到了结果。
var line_number = result.y
var column_number = result.x
Vector2I result = Search("print", (uint)TextEdit.SearchFlags.WholeWords, 0, 0);
if (result.X != -1)
{
// 找到了结果。
int lineNumber = result.Y;
int columnNumber = result.X;
}
void select ( int from_line, int from_column, int to_line, int to_column, int caret_index=0 )
执行选择,从行/列到行/列。
如果 selecting_enabled 为 false
,则不会发生选择。
void select_all ( )
选择所有文本。
如果 selecting_enabled 为 false
,则不会发生选择。
void select_word_under_caret ( int caret_index=-1 )
选中光标下的单词。
void set_caret_column ( int column, bool adjust_viewport=true, int caret_index=0 )
将光标移动到指定的 column
索引。
如果 adjust_viewport
为 true
,则视口将在移动发生后以光标位置为中心。
注意:如果支持多个光标,则不会检查任何重叠。请参阅 merge_overlapping_carets。
void set_caret_line ( int line, bool adjust_viewport=true, bool can_be_hidden=true, int wrap_index=0, int caret_index=0 )
将光标移动到指定的 line
索引。
如果 adjust_viewport
为 true
,则视口将在移动发生后以光标位置为中心。
如果 can_be_hidden
为 true
,则可以隐藏指定的 line
。
注意:如果支持多个光标,则不会检查任何重叠。见 merge_overlapping_carets。
void set_gutter_clickable ( int gutter, bool clickable )
将边栏设置为可点击。当鼠标在边栏上悬停时,会将鼠标光标变为指点的手形。
void set_gutter_custom_draw ( int column, Callable draw_callback )
为边栏设置自定义的绘制方法。回调方法必须接受以下参数:line: int, gutter: int, Area: Rect2
。
void set_gutter_draw ( int gutter, bool draw )
设置该边栏是否应被绘制。
void set_gutter_name ( int gutter, String name )
设置该边栏的名称。
void set_gutter_overwritable ( int gutter, bool overwritable )
设置该边栏为可覆写。见 merge_gutters。
void set_gutter_type ( int gutter, GutterType type )
设置边栏的类型。
void set_gutter_width ( int gutter, int width )
设置该边栏的宽度。
void set_line ( int line, String new_text )
设置特定行的文本。
void set_line_as_center_visible ( int line, int wrap_index=0 )
将 line
的 wrap_index
放置在视口的中心。
void set_line_as_first_visible ( int line, int wrap_index=0 )
将 line
的 wrap_index
放置在视口的顶部。
void set_line_as_last_visible ( int line, int wrap_index=0 )
将 line
的 wrap_index
放置在视口的底部。
void set_line_background_color ( int line, Color color )
设置该行的当前背景色。设置为 Color(0, 0, 0, 0)
则没有颜色。
void set_line_gutter_clickable ( int line, int gutter, bool clickable )
如果 clickable
为 true
,则让位于 line
的 gutter
可点击。见 gutter_clicked。
void set_line_gutter_icon ( int line, int gutter, Texture2D icon )
将边栏 gutter
在第 line
行的图标设置为 icon
。
void set_line_gutter_item_color ( int line, int gutter, Color color )
将边栏 gutter
在第 line
行的颜色设置为 color
。
void set_line_gutter_metadata ( int line, int gutter, Variant metadata )
将边栏 gutter
在第 line
行的元数据设置为 metadata
。
void set_line_gutter_text ( int line, int gutter, String text )
将边栏 gutter
在第 line
行的文本设置为 text
。
void set_overtype_mode_enabled ( bool enabled )
如果为 true
,则将用户设置为改写模式。当用户在该模式下键入时,它将覆盖现有文本。
void set_search_flags ( int flags )
设置搜索标志 flags
。配合 set_search_text 使用可以高亮被搜索的文本。搜索标志可以使用 SearchFlags 枚举指定。
void set_search_text ( String search_text )
设置搜索文本。见 set_search_flags。
void set_selection_mode ( SelectionMode mode, int line=-1, int column=-1, int caret_index=0 )
设置当前的选区模式。
void set_tab_size ( int size )
设置该 TextEdit 使用的制表符大小。
void set_tooltip_request_func ( Callable callback )
提供自定义工具提示文本。该回调方法必须接受以下参数:hovered_word: String
。
void start_action ( EditAction action )
开始一个动作,如果 action
与当前动作不同,则会终止当前动作。
调用 end_action、触发 ProjectSettings.gui/timers/text_edit_idle_detect_sec 或者在 start_action 和 end_action 之外调用可撤销的操作都会导致动作的终止。
void swap_lines ( int from_line, int to_line )
交换两行。
void tag_saved_version ( )
将当前版本标记为已保存。
void undo ( )
执行撤销操作。
主题属性说明¶
Color background_color = Color(0, 0, 0, 0)
设置该 TextEdit 的背景 Color。
Color caret_background_color = Color(0, 0, 0, 1)
使用块状光标时,光标下文本的 Color。
Color caret_color = Color(0.875, 0.875, 0.875, 1)
光标的 Color。可以设为完全透明的颜色,从而完全隐藏光标。
Color current_line_color = Color(0.25, 0.25, 0.26, 0.8)
光标所在行的背景 Color。
Color font_color = Color(0.875, 0.875, 0.875, 1)
设置字体颜色 Color。
Color font_outline_color = Color(1, 1, 1, 1)
该 TextEdit 的文本轮廓的色调。
Color font_placeholder_color = Color(0.875, 0.875, 0.875, 0.6)
placeholder_text 的字体颜色。
Color font_readonly_color = Color(0.875, 0.875, 0.875, 0.5)
Color font_selected_color = Color(0, 0, 0, 0)
设置所选文本的 Color。如果等于 Color(0, 0, 0, 0)
,则该属性将被忽略。
Color search_result_border_color = Color(0.3, 0.3, 0.3, 0.4)
与搜索查询匹配的文本周围的边框的 Color。
Color search_result_color = Color(0.3, 0.3, 0.3, 1)
与搜索查询匹配的文本背景的 Color。
Color selection_color = Color(0.5, 0.5, 0.5, 1)
设置文本选择的高亮 Color 颜色。
Color word_highlighted_color = Color(0.5, 0.5, 0.5, 0.25)
设置多次出现的高亮 Color 颜色。须启用 highlight_all_occurrences 。
int caret_width = 1
文本光标的像素宽度。较大的值能够确保光标易于看到,确保光标与较大的字体保持一致,从而提高可访问性。如果设置为 0
或更低,光标的宽度将被自动设置为 1 像素,并乘以显示缩放系数。
int line_spacing = 4
设置行间距。
int outline_size = 0
文字轮廓的大小。
注意:如果使用启用了 FontFile.multichannel_signed_distance_field 的字体,其 FontFile.msdf_pixel_range 必须至少设置为 outline_size 的两倍,轮廓渲染才能看起来正确。否则,轮廓可能会比预期的更早被切断。
Font font
设置默认的字体 Font。
int font_size
设置默认的字体大小。
Texture2D space
为空格文本字符,设置一个自定义 Texture2D。
Texture2D tab
为制表符文本字符,设置一个自定义 Texture2D。
StyleBox focus
设置当获得焦点时的 StyleBox。该 focus StyleBox 显示在基础 StyleBox 之上,因此应使用部分透明的 StyleBox 以确保基础 StyleBox 保持可见。表示轮廓或下划线的 StyleBox 非常适合此目的。要禁用焦点视觉效果,请指定一个 StyleBoxEmpty 资源。请注意,禁用焦点视觉效果会破坏键盘 / 控制器导航的可用性,出于可访问性原因,不建议这样做。
StyleBox normal
设置这个 TextEdit 的 StyleBox。
StyleBox read_only