pub struct Text { /* private fields */ }Expand description
A collection of styled text spans.
Text provides a high-level interface for working with styled text.
It stores spans (styled text units) and provides operations for:
- Appending and building text
- Applying base styles
- Splitting into lines
- Truncating and wrapping to widths
§Ownership
Text uses Cow<'static, str> for storage, which means:
- String literals are stored by reference (zero-copy)
- Owned strings are stored inline
- The API is ergonomic (no lifetime parameters on Text)
Implementations§
Source§impl Text
impl Text
Sourcepub fn raw(content: impl AsRef<str>) -> Self
pub fn raw(content: impl AsRef<str>) -> Self
Create text from a raw string (may contain newlines).
Sourcepub fn styled(content: impl AsRef<str>, style: Style) -> Self
pub fn styled(content: impl AsRef<str>, style: Style) -> Self
Create styled text from a string (may contain newlines).
Sourcepub fn from_lines(lines: impl IntoIterator<Item = Line>) -> Self
pub fn from_lines(lines: impl IntoIterator<Item = Line>) -> Self
Create text from multiple lines.
Sourcepub fn from_spans<'a>(spans: impl IntoIterator<Item = Span<'a>>) -> Self
pub fn from_spans<'a>(spans: impl IntoIterator<Item = Span<'a>>) -> Self
Create text from spans (single line).
Sourcepub fn from_segments<'a>(
segments: impl IntoIterator<Item = Segment<'a>>,
) -> Self
pub fn from_segments<'a>( segments: impl IntoIterator<Item = Segment<'a>>, ) -> Self
Create text from segments.
Sourcepub fn height_as_u16(&self) -> u16
pub fn height_as_u16(&self) -> u16
Get the number of lines as u16, saturating at u16::MAX.
Sourcepub fn measurement(&self) -> TextMeasurement
pub fn measurement(&self) -> TextMeasurement
Return bounds-based measurement for this text block.
Sourcepub fn style(&self) -> Option<Style>
pub fn style(&self) -> Option<Style>
Get the style of the first span, if any.
Returns None if the text is empty or has no styled spans.
Sourcepub fn push_span<'a>(&mut self, span: Span<'a>)
pub fn push_span<'a>(&mut self, span: Span<'a>)
Add a span to the last line (or create new line if empty).
Sourcepub fn with_span<'a>(self, span: Span<'a>) -> Self
pub fn with_span<'a>(self, span: Span<'a>) -> Self
Append a span to the last line (builder pattern).
Sourcepub fn apply_base_style(&mut self, base: Style)
pub fn apply_base_style(&mut self, base: Style)
Apply a base style to all lines and spans.
The base style is merged with each span’s style, with the span’s style taking precedence for conflicting properties.
Sourcepub fn with_base_style(self, base: Style) -> Self
pub fn with_base_style(self, base: Style) -> Self
Create a new Text with base style applied.
Sourcepub fn to_plain_text(&self) -> String
pub fn to_plain_text(&self) -> String
Get the plain text content (lines joined with newlines).
Sourcepub fn into_segment_lines(self) -> SegmentLines<'static>
pub fn into_segment_lines(self) -> SegmentLines<'static>
Convert to SegmentLines.
Trait Implementations§
Source§impl FromIterator<Line> for Text
impl FromIterator<Line> for Text
Source§impl<'a> FromIterator<Span<'a>> for Text
impl<'a> FromIterator<Span<'a>> for Text
Source§impl<'a> IntoIterator for &'a Text
impl<'a> IntoIterator for &'a Text
Source§impl IntoIterator for Text
impl IntoIterator for Text
impl Eq for Text
impl StructuralPartialEq for Text
Auto Trait Implementations§
impl Freeze for Text
impl RefUnwindSafe for Text
impl Send for Text
impl Sync for Text
impl Unpin for Text
impl UnwindSafe for Text
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.