pub struct Text { /* private fields */ }
Expand description
A piece of text that can be rendered.
Performance
The layout and geometry of the text is cached after the first time it is
calculated, making subsequent renders much faster. If your text stays
the same from frame to frame, reusing the Text
object will be much
faster than recreating it.
Examples
The text
example demonstrates how to load a font and then draw some text.
Implementations§
source§impl Text
impl Text
sourcepub fn new<C>(content: C, font: Font) -> Textwhere
C: Into<String>,
pub fn new<C>(content: C, font: Font) -> Textwhere C: Into<String>,
Creates a new Text
, with the given content and font.
sourcepub fn wrapped<C>(content: C, font: Font, max_width: f32) -> Textwhere
C: Into<String>,
pub fn wrapped<C>(content: C, font: Font, max_width: f32) -> Textwhere C: Into<String>,
Creates a new wrapped Text
, with the given content, font
and maximum width.
If a word is too long to fit, it may extend beyond the max width - use
get_bounds
if you need to find the actual bounds
of the text.
sourcepub fn draw<P>(&mut self, ctx: &mut Context, params: P)where
P: Into<DrawParams>,
pub fn draw<P>(&mut self, ctx: &mut Context, params: P)where P: Into<DrawParams>,
Draws the text to the screen (or to a canvas, if one is enabled).
sourcepub fn set_content<C>(&mut self, content: C)where
C: Into<String>,
pub fn set_content<C>(&mut self, content: C)where C: Into<String>,
Sets the content of the text.
Calling this function will cause a re-layout of the text the next time it is rendered.
sourcepub fn set_font(&mut self, font: Font)
pub fn set_font(&mut self, font: Font)
Sets the font of the text.
Calling this function will cause a re-layout of the text the next time it is rendered.
sourcepub fn max_width(&self) -> Option<f32>
pub fn max_width(&self) -> Option<f32>
Gets the maximum width of the text, if one is set.
If a word is too long to fit, it may extend beyond this width - use
get_bounds
if you need to find the actual bounds
of the text.
sourcepub fn set_max_width(&mut self, max_width: Option<f32>)
pub fn set_max_width(&mut self, max_width: Option<f32>)
Sets the maximum width of the text.
If Some
is passed, word-wrapping will be enabled. If None
is passed,
it will be disabled.
If a word is too long to fit, it may extend beyond this width - use
get_bounds
if you need to find the actual bounds
of the text.
Calling this function will cause a re-layout of the text the next time it is rendered.
sourcepub fn push(&mut self, ch: char)
pub fn push(&mut self, ch: char)
Appends the given character to the end of the text.
Calling this function will cause a re-layout of the text the next time it is rendered.
sourcepub fn push_str(&mut self, string: &str)
pub fn push_str(&mut self, string: &str)
Appends the given string slice to the end of the text.
Calling this function will cause a re-layout of the text the next time it is rendered.
sourcepub fn pop(&mut self) -> Option<char>
pub fn pop(&mut self) -> Option<char>
Removes the last character from the text and returns it.
Returns None
if the text is empty.
Calling this function will cause a re-layout of the text the next time it is rendered.
sourcepub fn get_bounds(&mut self, ctx: &mut Context) -> Option<Rectangle>
pub fn get_bounds(&mut self, ctx: &mut Context) -> Option<Rectangle>
Get the outer bounds of the text when rendered to the screen.
If the text’s layout needs calculating, this method will do so.
Note that this method will not take into account the positioning applied to the text via DrawParams
.