TextEdit

Inherits: Control < CanvasItem < Node < Object

多行文本编辑控件。

描述

TextEdit是用来编辑大型多行文本的。它也有编辑代码的功能,如支持语法高亮和多级撤销/重做。

注意:当按住Alt时,垂直滚轮的滚动速度是平时的5倍。这在Godot脚本编辑器中也适用。

属性

bool

bookmark_gutter

false

bool

breakpoint_gutter

false

bool

caret_blink

false

float

caret_blink_speed

0.65

bool

caret_block_mode

false

bool

caret_moving_by_right_click

true

bool

context_menu_enabled

true

bool

draw_spaces

false

bool

draw_tabs

false

FocusMode

focus_mode

2 (overrides Control)

bool

fold_gutter

false

bool

hiding_enabled

false

bool

highlight_all_occurrences

false

bool

highlight_current_line

false

bool

minimap_draw

false

int

minimap_width

80

CursorShape

mouse_default_cursor_shape

1 (overrides Control)

bool

override_selected_font_color

false

bool

readonly

false

int

scroll_horizontal

0

float

scroll_vertical

0.0

bool

selecting_enabled

true

bool

shortcut_keys_enabled

true

bool

show_line_numbers

false

bool

smooth_scrolling

false

bool

syntax_highlighting

false

String

text

“”

float

v_scroll_speed

80.0

bool

virtual_keyboard_enabled

true

bool

wrap_enabled

false

方法

void

add_color_region ( String begin_key, String end_key, Color color, bool line_only=false )

void

add_keyword_color ( String keyword, Color color )

bool

can_fold ( int line ) const

void

center_viewport_to_cursor ( )

void

clear_colors ( )

void

clear_undo_history ( )

void

copy ( )

int

cursor_get_column ( ) const

int

cursor_get_line ( ) const

void

cursor_set_column ( int column, bool adjust_viewport=true )

void

cursor_set_line ( int line, bool adjust_viewport=true, bool can_be_hidden=true, int wrap_index=0 )

void

cut ( )

void

deselect ( )

void

fold_all_lines ( )

void

fold_line ( int line )

Array

get_breakpoints ( ) const

Color

get_keyword_color ( String keyword ) const

String

get_line ( int line ) const

Vector2

get_line_column_at_pos ( Vector2 position ) const

int

get_line_count ( ) const

int

get_line_height ( ) const

int

get_line_width ( int line, int wrap_index=-1 ) const

int

get_line_wrap_count ( int line ) const

PoolStringArray

get_line_wrapped_text ( int line ) const

PopupMenu

get_menu ( ) const

Vector2

get_pos_at_line_column ( int line, int column ) const

Rect2

get_rect_at_line_column ( int line, int column ) const

int

get_selection_from_column ( ) const

int

get_selection_from_line ( ) const

String

get_selection_text ( ) const

int

get_selection_to_column ( ) const

int

get_selection_to_line ( ) const

int

get_total_gutter_width ( ) const

String

get_word_under_cursor ( ) const

bool

has_keyword_color ( String keyword ) const

bool

has_redo ( ) const

bool

has_undo ( ) const

void

insert_text_at_cursor ( String text )

bool

is_folded ( int line ) const

bool

is_line_hidden ( int line ) const

bool

is_line_set_as_bookmark ( int line ) const

bool

is_line_set_as_breakpoint ( int line ) const

bool

is_line_set_as_safe ( int line ) const

bool

is_line_wrapped ( int line ) const

bool

is_selection_active ( ) const

void

menu_option ( int option )

void

paste ( )

void

redo ( )

void

remove_breakpoints ( )

PoolIntArray

search ( String key, int flags, int from_line, int from_column ) const

void

select ( int from_line, int from_column, int to_line, int to_column )

void

select_all ( )

void

set_line ( int line, String new_text )

void

set_line_as_bookmark ( int line, bool bookmark )

void

set_line_as_breakpoint ( int line, bool breakpoint )

void

set_line_as_hidden ( int line, bool enable )

void

set_line_as_safe ( int line, bool safe )

void

toggle_fold_line ( int line )

void

undo ( )

void

unfold_line ( int line )

void

unhide_all_lines ( )

主题属性

Color

background_color

Color( 0, 0, 0, 0 )

Color

bookmark_color

Color( 0.08, 0.49, 0.98, 1 )

Color

brace_mismatch_color

Color( 1, 0.2, 0.2, 1 )

Color

breakpoint_color

Color( 0.8, 0.8, 0.4, 0.2 )

Color

caret_background_color

Color( 0, 0, 0, 1 )

Color

caret_color

Color( 0.88, 0.88, 0.88, 1 )

Color

code_folding_color

Color( 0.8, 0.8, 0.8, 0.8 )

Color

completion_background_color

Color( 0.17, 0.16, 0.2, 1 )

Color

completion_existing_color

Color( 0.87, 0.87, 0.87, 0.13 )

Color

completion_font_color

Color( 0.67, 0.67, 0.67, 1 )

Color

completion_scroll_color

Color( 1, 1, 1, 1 )

Color

completion_selected_color

Color( 0.26, 0.26, 0.27, 1 )

Color

current_line_color

Color( 0.25, 0.25, 0.26, 0.8 )

Color

executing_line_color

Color( 0.2, 0.8, 0.2, 0.4 )

Color

font_color

Color( 0.88, 0.88, 0.88, 1 )

Color

font_color_readonly

Color( 0.88, 0.88, 0.88, 0.5 )

Color

font_color_selected

Color( 0, 0, 0, 1 )

Color

function_color

Color( 0.4, 0.64, 0.81, 1 )

Color

line_number_color

Color( 0.67, 0.67, 0.67, 0.4 )

Color

mark_color

Color( 1, 0.4, 0.4, 0.4 )

Color

member_variable_color

Color( 0.9, 0.31, 0.35, 1 )

Color

number_color

Color( 0.92, 0.58, 0.2, 1 )

Color

safe_line_number_color

Color( 0.67, 0.78, 0.67, 0.6 )

Color

selection_color

Color( 0.49, 0.49, 0.49, 1 )

Color

symbol_color

Color( 0.94, 0.94, 0.94, 1 )

Color

word_highlighted_color

Color( 0.8, 0.9, 0.9, 0.15 )

int

completion_lines

7

int

completion_max_width

50

int

completion_scroll_width

3

int

line_spacing

4

Font

font

Texture

fold

Texture

folded

Texture

space

Texture

tab

StyleBox

completion

StyleBox

focus

StyleBox

normal

StyleBox

read_only

信号

  • breakpoint_toggled ( int row )

当通过断点栏放置断点时发出。


  • cursor_changed ( )

光标更改时发出。


单击信息图标时发出。


  • request_completion ( )


  • text_changed ( )

当文本更改时触发。

枚举

enum SearchFlags:

  • SEARCH_MATCH_CASE = 1 —- 搜索时匹配大小写。

  • SEARCH_WHOLE_WORDS = 2 —- 搜索时匹配整个单词。

  • SEARCH_BACKWARDS = 4 —- 从头到尾搜索。


enum SearchResult:

  • SEARCH_RESULT_COLUMN = 0 —- 用来访问搜索 search 的结果列。

  • SEARCH_RESULT_LINE = 1 —- 用于访问搜索 search 的结果行。


enum MenuItems:

  • MENU_CUT = 0 —- 剪切(复制并删除)选定的文本。

  • MENU_COPY = 1 —- 复制选定的文本。

  • MENU_PASTE = 2 —- 将剪贴板上的文本粘贴在选定的文本上(或光标的位置)。

  • MENU_CLEAR = 3 —- 擦除整个 TextEdit 文本。

  • MENU_SELECT_ALL = 4 —- 选择整个 TextEdit 文本。

  • MENU_UNDO = 5 —- 撤销之前的操作。

  • MENU_REDO = 6 —- 重做前一个动作。

  • MENU_MAX = 7 —- 表示MenuItems枚举的大小。

属性说明

  • bool bookmark_gutter

Default

false

Setter

set_bookmark_gutter_enabled(value)

Getter

is_bookmark_gutter_enabled()

true 时书签栏可见。


  • bool breakpoint_gutter

Default

false

Setter

set_breakpoint_gutter_enabled(value)

Getter

is_breakpoint_gutter_enabled()

true 时断点栏可见。


Default

false

Setter

cursor_set_blink_enabled(value)

Getter

cursor_get_blink_enabled()

如果为true,插入符号(可视光标)将闪烁。


Default

0.65

Setter

cursor_set_blink_speed(value)

Getter

cursor_get_blink_speed()

插入符号闪烁周期的持续时间(秒)。


  • bool caret_block_mode

Default

false

Setter

cursor_set_block_mode(value)

Getter

cursor_is_block_mode()

true 时光标显示为矩形。

false 时光标显示为条形。


  • bool caret_moving_by_right_click

Default

true

Setter

set_right_click_moves_caret(value)

Getter

is_right_click_moving_caret()

true 时,在显示上下文菜单之前右键单击将光标移动到鼠标位置。

false 时,上下文菜单忽略鼠标位置。


  • bool context_menu_enabled

Default

true

Setter

set_context_menu_enabled(value)

Getter

is_context_menu_enabled()

true 时右键单击会显示上下文菜单。


Default

false

Setter

set_draw_spaces(value)

Getter

is_drawing_spaces()

如果 true,“空格”字符将具有可见表示形式。


Default

false

Setter

set_draw_tabs(value)

Getter

is_drawing_tabs()

如果 true,“制表符”字符将具有可见表示形式。


Default

false

Setter

set_draw_fold_gutter(value)

Getter

is_drawing_fold_gutter()

如果 true,则折叠栏可见。这允许折叠缩进行。


  • bool hiding_enabled

Default

false

Setter

set_hiding_enabled(value)

Getter

is_hiding_enabled()

如果 true,所有已被 set_line_as_hidden 设置为隐藏的行将不可见。


  • bool highlight_all_occurrences

Default

false

Setter

set_highlight_all_occurrences(value)

Getter

is_highlight_all_occurrences_enabled()

如果 true,所选文本的所有匹配项都将突出显示。


  • bool highlight_current_line

Default

false

Setter

set_highlight_current_line(value)

Getter

is_highlight_current_line_enabled()

如果 true,则包含光标的行会突出显示。


Default

false

Setter

draw_minimap(value)

Getter

is_drawing_minimap()

如果 true,则显示小地图,提供源代码的概要。


  • int minimap_width

Default

80

Setter

set_minimap_width(value)

Getter

get_minimap_width()

小地图的宽度(以像素为单位)。


  • bool override_selected_font_color

Default

false

Setter

set_override_selected_font_color(value)

Getter

is_overriding_selected_font_color()

如果 true,自定义 font_color_selected 将用于所选文本。


Default

false

Setter

set_readonly(value)

Getter

is_readonly()

如果true,启用只读模式。现有的文本不能被修改,新的文本不能被添加。


  • int scroll_horizontal

Default

0

Setter

set_h_scroll(value)

Getter

get_h_scroll()

如果有一个水平滚动条,这决定了当前的水平滚动值,单位是像素。


Default

0.0

Setter

set_v_scroll(value)

Getter

get_v_scroll()

如果有一个垂直滚动条,这就决定了当前的垂直滚动值,以行数为单位,顶行从0开始。


  • bool selecting_enabled

Default

true

Setter

set_selecting_enabled(value)

Getter

is_selecting_enabled()

如果true,文本可以被选择。

如果false,用户或使用selectselect_all方法都不能选择文本。


  • bool shortcut_keys_enabled

Default

true

Setter

set_shortcut_keys_enabled(value)

Getter

is_shortcut_keys_enabled()

true 时,即使上下文菜单已被禁用,也会启用该上下文菜单的快捷键。


  • bool show_line_numbers

Default

false

Setter

set_show_line_numbers(value)

Getter

is_show_line_numbers_enabled()

如果 true,行号显示在文本的左侧。


  • bool smooth_scrolling

Default

false

Setter

set_smooth_scroll_enable(value)

Getter

is_smooth_scroll_enabled()

如果 true,将滚动条的步长 step 设置为 0.25,从而使滚动更加平滑。


  • bool syntax_highlighting

Default

false

Setter

set_syntax_coloring(value)

Getter

is_syntax_coloring_enabled()

如果 true,为此 TextEdit 设置的任何自定义颜色属性都将可见。


Default

“”

Setter

set_text(value)

Getter

get_text()

TextEdit 的字符串值。


Default

80.0

Setter

set_v_scroll_speed(value)

Getter

get_v_scroll_speed()

垂直滚动的灵敏度。


  • bool virtual_keyboard_enabled

Default

true

Setter

set_virtual_keyboard_enabled(value)

Getter

is_virtual_keyboard_enabled()

如果true,则本机虚拟键盘将显示在支持它的平台上。


Default

false

Setter

set_wrap_enabled(value)

Getter

is_wrap_enabled()

如果 true,当文本超出可见边缘时启用文本换行。

方法说明

添加给定分隔符划分区域的颜色。


  • void add_keyword_color ( String keyword, Color color )

添加一个关键字keyword和它的颜色Color


返回给定的行是否可折叠,也就是说,它在它的正下方有缩进的行。


  • void center_viewport_to_cursor ( )

将视窗置于编辑光标所在的线上。这也会将 scroll_horizontal 值重置为 0


  • void clear_colors ( )

清除之前用add_color_regionadd_keyword_color添加的所有自定义语法着色信息。


  • void clear_undo_history ( )

清除撤销历史。


  • void copy ( )

复制当前的文本选择。


  • int cursor_get_column ( ) const

返回编辑光标所在的列。


  • int cursor_get_line ( ) const

返回编辑光标所在的行。


  • void cursor_set_column ( int column, bool adjust_viewport=true )

将光标移动到指定的 column 索引处。

如果 adjust_viewport 设置为 true,则移动发生后视窗将以光标位置为中心。


  • void cursor_set_line ( int line, bool adjust_viewport=true, bool can_be_hidden=true, int wrap_index=0 )

在指定的 line 索引处移动光标。

如果 adjust_viewport 设置为 true,则移动发生后视窗将以光标位置为中心。

如果 can_be_hidden 设置为 true,则可以使用 set_line_as_hidden 隐藏指定的 line


  • void cut ( )

剪切当前选择。


  • void deselect ( )

取消当前选择。


  • void fold_all_lines ( )

折叠所有可能被折叠的行(参阅 can_fold)。


  • void fold_line ( int line )

如果可能,折叠给定的行(参阅 can_fold)。


  • Array get_breakpoints ( ) const

返回一个包含每个断点行号的数组。


返回指定的关键字keyword的颜色Color


返回特定行的文本。


返回位于给定位置的行和列。返回的向量中,x 是列、y 是行。


  • int get_line_count ( ) const

返回文本中的总行数。


  • int get_line_height ( ) const

返回最大行的高度。


  • int get_line_width ( int line, int wrap_index=-1 ) const

返回位于行 linewrap_index 的像素宽度。


  • int get_line_wrap_count ( int line ) const

返回给定行换行的次数。


返回代表各个换行索引的 String 数组。


返回此TextEditPopupMenu。默认情况下,这个菜单在右键点击TextEdit的时候显示。

警告: 这是一个必要的内部节点,删除和释放它可能会导致崩溃。如果你想隐藏它或它的任何子节点,请使用其的 CanvasItem.visible 属性。


返回给定行 line 和列 column 的本地位置。如果返回向量的 xy 等于 -1,该位置位于该控件的可视范围之外。

注意:位置的 Y 对应行的底部。请使用 get_rect_at_line_column 来获取顶部位置。


  • Rect2 get_rect_at_line_column ( int line, int column ) const

返回位于给定行 line 和列 column 的字形大小。如果返回的矩形位置的 xy 等于 -1,该位置位于该控件的可视范围之外。

注意:位置的 Y 对应行的顶部,而 get_pos_at_line_column 返回的则是底部位置。


  • int get_selection_from_column ( ) const

返回选择的开始列。


  • int get_selection_from_line ( ) const

返回选择开始行。


  • String get_selection_text ( ) const

返回选择内的文本。


  • int get_selection_to_column ( ) const

返回选择结束列。


  • int get_selection_to_line ( ) const

返回选择结束行。


  • int get_total_gutter_width ( ) const

返回所有栏位及内部边距的总宽度。


  • String get_word_under_cursor ( ) const

返回一个 String 文本,该文本中的单词位于 caret(文本光标)的位置。


返回指定的关键字 keyword 是否有一个颜色被设置为它。


  • bool has_redo ( ) const

有“重做”动作可用时返回 true


  • bool has_undo ( ) const

有“撤销”动作可用时返回 true


  • void insert_text_at_cursor ( String text )

在光标位置插入指定的文本。


返回指定索引处的行是否折叠。


  • bool is_line_hidden ( int line ) const

返回指定索引处的行是否隐藏。


  • bool is_line_set_as_bookmark ( int line ) const

当指定的行 line 被标记为书签时,返回 true


  • bool is_line_set_as_breakpoint ( int line ) const

当指定的行 line 有断点时返回 true


  • bool is_line_set_as_safe ( int line ) const

当指定的行 line 被标记为安全时返回 true


  • bool is_line_wrapped ( int line ) const

返回给定的行是否换行。


  • bool is_selection_active ( ) const

如果选择处于活动状态,则返回 true


  • void menu_option ( int option )

按指定的索引触发右键单击菜单操作。有关可用索引的列表,请参阅 MenuItems


  • void paste ( )

粘贴当前选择。


  • void redo ( )

执行重做操作。


  • void remove_breakpoints ( )

删除所有断点。这不会触发 breakpoint_toggled 信号。


在文本内部进行搜索。搜索标志可以在SearchFlags枚举中指定。

如果没有找到结果,返回一个空的PoolIntArray。否则,可以通过SearchResult枚举中指定的索引访问结果行和列,例如。

  1. var result = search(key, flags, line, column)
  2. if result.size() > 0:
  3. # 找到的结果。
  4. var res_line = result[TextEdit.SEARCH_RESULT_LINE)
  5. var res_column = result[TextEdit.SEARCH_RESULT_COLUMN]

  • void select ( int from_line, int from_column, int to_line, int to_column )

执行选择,从行/列到行/列。

如果 selecting_enabledfalse,则不会发生选择。


  • void select_all ( )

选择所有文本。

如果 selecting_enabledfalse,则不会发生选择。


设置特定行的文本。


  • void set_line_as_bookmark ( int line, bool bookmark )

如果 bookmark 为 true,则为行 line 设置书签。如果 bookmark 为 false,则删除该书签。

书签显示在 breakpoint_gutter 中。


  • void set_line_as_breakpoint ( int line, bool breakpoint )

添加或删除 line 中的断点。断点显示在 breakpoint_gutter 中。


  • void set_line_as_hidden ( int line, bool enable )

如果 true,隐藏指定索引的行。


  • void set_line_as_safe ( int line, bool safe )

如果为 true,将行 line 标记为安全。

这将以 safe_line_number_color 主题属性中提供的颜色显示行号。


  • void toggle_fold_line ( int line )

在给定行切换代码块的折叠。


  • void undo ( )

执行撤销操作。


  • void unfold_line ( int line )

如果折叠,展开给定的线。


  • void unhide_all_lines ( )

取消隐藏之前由 set_line_as_hidden 设置为隐藏的所有行。

Theme Property Descriptions

Default

Color( 0, 0, 0, 0 )

设置这个 TextEdit 的背景颜色 Color。必须启用语法高亮 syntax_highlighting


Default

Color( 0.08, 0.49, 0.98, 1 )

设置书签标记的颜色 Color。必须启用语法高亮 syntax_highlighting


  • Color brace_mismatch_color

Default

Color( 1, 0.2, 0.2, 1 )


Default

Color( 0.8, 0.8, 0.4, 0.2 )

设置断点的颜色 Color。必须启用断点栏 breakpoint_gutter


  • Color caret_background_color

Default

Color( 0, 0, 0, 1 )


Default

Color( 0.88, 0.88, 0.88, 1 )


  • Color code_folding_color

Default

Color( 0.8, 0.8, 0.8, 0.8 )


  • Color completion_background_color

Default

Color( 0.17, 0.16, 0.2, 1 )


  • Color completion_existing_color

Default

Color( 0.87, 0.87, 0.87, 0.13 )


  • Color completion_font_color

Default

Color( 0.67, 0.67, 0.67, 1 )


  • Color completion_scroll_color

Default

Color( 1, 1, 1, 1 )


  • Color completion_selected_color

Default

Color( 0.26, 0.26, 0.27, 1 )


  • Color current_line_color

Default

Color( 0.25, 0.25, 0.26, 0.8 )

设置断点的颜色 Color。必须启用断点栏 breakpoint_gutter


  • Color executing_line_color

Default

Color( 0.2, 0.8, 0.2, 0.4 )


Default

Color( 0.88, 0.88, 0.88, 1 )

设置字体颜色 Color


  • Color font_color_readonly

Default

Color( 0.88, 0.88, 0.88, 0.5 )


  • Color font_color_selected

Default

Color( 0, 0, 0, 1 )

设置所选文本的颜色 Color。必须启用覆盖所选文字字体颜色 override_selected_font_color


Default

Color( 0.4, 0.64, 0.81, 1 )


Default

Color( 0.67, 0.67, 0.67, 0.4 )

设置行号颜色 Colorshow_line_numbers 显示行号必须被启用。


Default

Color( 1, 0.4, 0.4, 0.4 )

设置标记文本的颜色 Color


  • Color member_variable_color

Default

Color( 0.9, 0.31, 0.35, 1 )


Default

Color( 0.92, 0.58, 0.2, 1 )


  • Color safe_line_number_color

Default

Color( 0.67, 0.78, 0.67, 0.6 )


Default

Color( 0.49, 0.49, 0.49, 1 )

设置文本选择的高亮 Color 颜色。


Default

Color( 0.94, 0.94, 0.94, 1 )


  • Color word_highlighted_color

Default

Color( 0.8, 0.9, 0.9, 0.15 )

设置多次出现的高亮 Color 颜色。须启用 highlight_all_occurrences


  • int completion_lines

Default

7


  • int completion_max_width

Default

50


  • int completion_scroll_width

Default

3


  • int line_spacing

Default

4

设置行间距。


设置默认的字体 Font





为制表符字符设置自定义纹理 Texture




设置这个 TextEditStyleBox


readonly 启用时,设置这个 TextEditStyleBox