zng

Module text

source
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.

§Full API

See zng_wgt_text for the full widget API.

Modules§

  • Commands that control the editable text.

Macros§

  • A simple text run with italic font style.
  • A simple text run with bold font weight.
  • Creates a Txt by formatting using the format_args! syntax.

Structs§

Enums§

Statics§

Traits§

  • A trait for converting a value to a Txt.
  • Represents a type that can be a var value, parse and display.

Functions§