Expand description
A string split over multiple lines where each line is composed of several clusters, each with their own style.
A Text
, like a Span
, can be constructed using one of the many From
implementations
or via the Text::raw
and Text::styled
methods. Helpfully, Text
also implements
core::iter::Extend
which enables the concatenation of several Text
blocks.
let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC);
// An initial two lines of `Text` built from a `&str`
let mut text = Text::from("The first line\nThe second line");
assert_eq!(2, text.height());
// Adding two more unstyled lines
text.extend(Text::raw("These are two\nmore lines!"));
assert_eq!(4, text.height());
// Adding a final two styled lines
text.extend(Text::styled("Some more lines\nnow with more style!", style));
assert_eq!(6, text.height());
Fields
lines: Vec<Spans<'a>>
Implementations
sourceimpl<'a> Text<'a>
impl<'a> Text<'a>
sourcepub fn raw<T>(content: T) -> Text<'a> where
T: Into<Cow<'a, str>>,
pub fn raw<T>(content: T) -> Text<'a> where
T: Into<Cow<'a, str>>,
Create some text (potentially multiple lines) with no style.
Examples
Text::raw("The first line\nThe second line");
Text::raw(String::from("The first line\nThe second line"));
sourcepub fn styled<T>(content: T, style: Style) -> Text<'a> where
T: Into<Cow<'a, str>>,
pub fn styled<T>(content: T, style: Style) -> Text<'a> where
T: Into<Cow<'a, str>>,
Create some text (potentially multiple lines) with a style.
Examples
let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC);
Text::styled("The first line\nThe second line", style);
Text::styled(String::from("The first line\nThe second line"), style);
sourcepub fn width(&self) -> usize
pub fn width(&self) -> usize
Returns the max width of all the lines.
Examples
use tui::text::Text;
let text = Text::from("The first line\nThe second line");
assert_eq!(15, text.width());
sourcepub fn height(&self) -> usize
pub fn height(&self) -> usize
Returns the height.
Examples
use tui::text::Text;
let text = Text::from("The first line\nThe second line");
assert_eq!(2, text.height());
sourcepub fn patch_style(&mut self, style: Style)
pub fn patch_style(&mut self, style: Style)
Apply a new style to existing text.
Examples
let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC);
let mut raw_text = Text::raw("The first line\nThe second line");
let styled_text = Text::styled(String::from("The first line\nThe second line"), style);
assert_ne!(raw_text, styled_text);
raw_text.patch_style(style);
assert_eq!(raw_text, styled_text);
Trait Implementations
sourceimpl<'a> Extend<Spans<'a>> for Text<'a>
impl<'a> Extend<Spans<'a>> for Text<'a>
sourcefn extend<T: IntoIterator<Item = Spans<'a>>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = Spans<'a>>>(&mut self, iter: T)
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<'a> IntoIterator for Text<'a>
impl<'a> IntoIterator for Text<'a>
impl<'a> Eq for Text<'a>
impl<'a> StructuralEq for Text<'a>
impl<'a> StructuralPartialEq for Text<'a>
Auto Trait Implementations
impl<'a> RefUnwindSafe for Text<'a>
impl<'a> Send for Text<'a>
impl<'a> Sync for Text<'a>
impl<'a> Unpin for Text<'a>
impl<'a> UnwindSafe for Text<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more