Expand description
Text widget, properties and other types.
The Text! widget implements text layout and rendering, it is also the base widget for
SelectableText!, TextInput! and label!. Text properties are largely contextual,
you can set text::font_size in any widget to affect all text inside that widget.
The Text! widget provides simple text rendering, that is all text is of the same style and
different fonts are only used as fallback. You can implement rich text by combining multiple
Text! and Wrap! panels, see the wrap module docs for an example. Some widgets also parse
text and generate the rich text setup automatically, the Markdown! and AnsiText! widgets
are examples of this.
The example below declares two text widgets, one displays a text that requires multiple fonts to render, the other displays debug information about the first.
use zng::prelude::*;
let txt = "text テキスト 📋";
let font_use = var(vec![]);
Stack! {
text::font_family = ["Segoe UI", "Yu Gothic UI", "Segoe Ui Emoji", "sans-serif"];
children = ui_vec![
Text! {
font_size = 1.5.em();
txt;
get_font_use = font_use.clone();
},
Text! {
font_size = 0.9.em();
txt = font_use.map(|u| {
let mut r = Txt::from("");
for (font, range) in u {
use std::fmt::Write as _;
writeln!(&mut r, "{} = {:?}", font.face().family_name(), &txt[range.clone()]).unwrap();
}
r.end_mut();
r
});
},
];
direction = StackDirection::top_to_bottom();
spacing = 15;
}Note that the font_family is set on the parent widget, both texts have the same
font family value because of this, the font_size on the other hand is set for
each text widget and only affects that widget.
§Rich Text
Rich text is a sequence of Text! of different styles and other widgets such as Image! inside one or more layout panels,
usually a Wrap! panel for paragraphs and a Stack! panel for the full text or page, if you only intent to present the text
that is all you need, the inline layout will coordinate the flow of lines across multiple Text! widgets.
To enable selection or editing in rich text you can enable the rich_text property on the panels. The outer panel will
declare a rich text context that the inner Text! widgets will use to coordinate the caret position and selection across texts.
use zng::prelude::*;
Wrap! {
text::rich_text = true;
text::txt_selectable = true;
children = ui_vec![
Text! {
txt = "red text";
font_color = colors::RED;
},
Text! {
txt = " green text";
font_color = colors::GREEN;
},
Text! {
txt = " blue text";
font_color = colors::BLUE;
},
];
}The example above declares a rich text with three different text runs, by enabling rich_text the wrap panel becomes
a rich text context that all descendant texts will use to coordinate text operations. In this case the txt_selectable
property enables text selection (and copy) for all descendants, without rich_text the descendant texts would allow
selection only within each text.
The rich_text property together with txt_editable is the base for rich text editor widgets, out of the box the descendant texts
will coordinate the caret position and the focused text is edited by typing. A rich text editor needs to implement many other features,
such as removing empty text widgets, inserting new styled texts, encoding all these texts into an unified representation for saving.
To suppress the default behavior of component texts you can handle keyboard events in the preview track and stop propagation,
same for mouse/touch events. The full text API crate provides the zng_wgt_text::cmd module that can be used to programmatically
control the texts. The active component text is just the focused widget, that can be controlled using the zng::focus module.
§Full API
See zng_wgt_text for the full widget API.
Modules§
- cmd
- Commands that controls the editable/selectable text.
Macros§
- Em
- A simple text run with italic font style.
- Strong
- A simple text run with bold font weight.
- formatx
- Creates a
Txtby formatting using theformat_args!syntax.
Structs§
- Auto
Selection - Defines when text is auto-selected on focus change.
- Caret
Status - Display info of edit caret position.
- Change
Stop Args - Arguments for
on_change_stop. - LangMix
mLanguage and text direction properties.- Paragraph
Mix mText paragraph properties.- Selection
Toolbar Args - Arguments for
selection_toolbar_fn. - TEXT
- Text internals used by text implementer nodes and properties.
- Text
WA configured text run.- Txt
- Text string type, can be one of multiple internal representations, mostly optimized for sharing and one for editing.
- Underline
Skip - Represents what parts of a text the underline must skip over.
Enums§
- Caret
Shape - Defines the position of an interactive caret in relation to the selection.
- Change
Stop Cause - Cause of an
on_change_stop. - Interactive
Caret Mode - Defines when the interactive carets are used.
- Lines
Wrap Count - Represents the number of lines and number of wrap lines in a text.
- Text
Overflow - Defines how text overflow is handled by the text widgets.
- TxtRepr
- Identifies how a
Txtis currently storing the string data. - Underline
Position - Defines what line gets traced by the text underline decoration.
Statics§
- FONT_
COLOR_ VAR - Color of
Text!glyphs that are not colored by palette.
Traits§
- ToTxt
- A trait for converting a value to a
Txt. - TxtParse
Value - Represents a type that can be a var value, parse and display.
Functions§
- accepts_
enter PIf the'\n'character is inserted when enter is pressed and the text is editable.- accepts_
tab PIf the'\t'character is inserted when tab is pressed and the text is editable.- auto_
selection PAuto-selection on focus change when the text is selectable.- caret_
color PDefines the color of the non-interactive caret.- change_
stop_ delay PDebounce time foron_change_stop.- direction
PSets the layout direction used in the layout of the widget and descendants.- font_aa
PConfigure the anti-aliasing used to render text glyphs inside the widget.- font_
annotation PSets the font annotation alternative feature.- font_
caps PSets the font capital variant features.- font_
char_ variant PSets the font character variant alternative feature.- font_
cn_ variant PSets the Chinese logographic set.- font_
color PDefines the color the most text glyphs are filled with.- font_
common_ lig PSets the font common ligatures features.- font_
contextual_ alt PSets the font contextual alternatives feature.- font_
discretionary_ lig PSets the font discretionary ligatures feature.- font_
ea_ width PSets the East Asian figure width.- font_
family PFont family name or list of names for texts in this widget or descendants.- font_
features PSets font features.- font_
historical_ forms PSets the font historical forms alternative feature.- font_
historical_ lig PSets the font historical ligatures feature.- font_
jp_ variant PSets the Japanese logographic set.- font_
kerning PSets the font kerning feature.- font_
num_ fraction PSets the font numeric fraction features.- font_
num_ spacing PSets the font numeric spacing features.- font_
numeric PSets the font numeric variant features.- font_
ornaments PSets the font ornaments alternative feature.- font_
palette PDefines the palette used to render colored glyphs (Emoji).- font_
palette_ colors PDefines custom palette colors that affect Emoji colors.- font_
position PSets the font sub/super script position alternative feature.- font_
size PSets the font size for the widget and descendants.- font_
stretch PDefines how condensed or expanded the preferred font should be.- font_
style PDefines the skew style of the font glyphs.- font_
style_ set PSets the font stylistic set alternative feature.- font_
stylistic PSets the font stylistic alternative feature.- font_
swash PSets the font swash features.- font_
synthesis PConfigure if a synthetic font is generated for fonts that do not implement bold or oblique variants.- font_
variations PSets font variations.- font_
weight PDefines the thickness or boldness the preferred font should have.- get_
caret_ index PGets the caret char index, if the text is editable.- get_
caret_ status PGets the caret display status, if the text is editable.- get_
chars_ count PGets the number of character in the text.- get_
lines_ len PGets the number of lines in the text, including wrap lines.- get_
lines_ wrap_ count PGets the number of wrap lines per text lines.- get_
overflow PGets the overflow text, that is a clone of the text starting from the first overflow character.- has_
selection PIf widget or rich context has selection.- hyphen_
char PThe char or small string that is rendered when text is auto-hyphenated.- hyphens
PConfigure hyphenation.- ime_
underline PStyle and thickness of the line drawn under the IME preview text.- interactive_
caret PDefines when the interactive carets are used.- interactive_
caret_ visual PDefines custom caret visual for interactive caret.- is_
line_ overflown PGets if the text has an entire line overflown.- is_
overflown PGets if the text is overflown.- is_
parse_ pending PIf text has changed buttxt_parsehas not tried to parse the new text yet.- justify_
mode PConfig the automatic spacing inserted between words and letters when text is aligned to fill.- lang
PSets the text language and script for the widget and descendants.- letter_
spacing PExtra spacing added in between text letters.- line_
break PConfiguration of text wrapping for Chinese, Japanese, or Korean text.- line_
height PHeight of each text line.- line_
spacing PExtra spacing in-between text lines.- max_
chars_ count PMaximum number of characters that can be input.- obscure_
txt PEnable typed text obscuring in render.- obscuring_
char PReplacement character used when obscuring text.- on_
change_ stop PCalled after the text changed and interaction has stopped.- overline
PDraw lines above each text line.- overline_
color PCustomoverlinecolor, if not set thefont_coloris used.- paragraph_
spacing PExtra spacing in-between paragraphs.- rich_
text PDefines a rich text context.- selection_
color PSets theSELECTION_COLOR_VAR.- selection_
toolbar PDefines the floating mini-toolbar that shows near a new text selection.- selection_
toolbar_ anchor PPosition the selection toolbar in relation to the bounding box of all selection rectangles.- selection_
toolbar_ fn PDefines the floating mini-toolbar that shows near a new text selection.- set_
interactive_ caret_ spot - Set the caret hotspot that marks the middle of the caret on the text line.
- strikethrough
PDraw lines across each text line.- strikethrough_
color PCustomstrikethroughcolor, if not set thefont_coloris used.- tab_
length PLength of the TAB character space, relative to the normal space advance.- txt_
align PAlignment of text inside available space.- txt_
editable PEnable text caret, input and makes the widget focusable.- txt_
overflow PDefines if text overflow is truncated, with optional suffix append.- txt_
overflow_ align PAlignment of text inside available space when the text overflows.- txt_
selectable PEnable text selection, copy and makes the widget focusable.- txt_
selectable_ alt_ only POnly start mouse and touch selections from this widget when the Alt keyboard modifier is pressed.- underline
PDraw lines under each text line.- underline_
color PCustomunderlinecolor, if not set thefont_coloris used.- underline_
skip PDefines what segments of each text line are skipped when tracing theunderline.- white_
space PText white space transform.- word_
break PConfigure line breaks inside words during text wrap.- word_
spacing PExtra spacing added to the UnicodeU+0020 SPACEcharacter.