Struct druid::text::RichTextBuilder
source · pub struct RichTextBuilder { /* private fields */ }
Expand description
A builder for creating RichText
objects.
This builder allows you to construct a RichText
object by building up a sequence
of styled sub-strings; first you push
a &str
onto the string,
and then you can optionally add styles to that text via the returned AttributesAdder
object.
Example
let mut builder = RichTextBuilder::new();
builder.push("Hello ");
builder.push("World!").weight(FontWeight::BOLD);
// Can also use write!
write!(builder, "Here is your number: {}", 1).underline(true).text_color(Color::RED);
let rich_text = builder.build();
Implementations§
source§impl RichTextBuilder
impl RichTextBuilder
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new RichTextBuilder
.
Examples found in repository?
examples/markdown_preview.rs (line 141)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
fn rebuild_rendered_text(text: &str) -> RichText {
let mut current_pos = 0;
let mut builder = RichTextBuilder::new();
let mut tag_stack = Vec::new();
let parser = Parser::new_ext(text, Options::ENABLE_STRIKETHROUGH);
for event in parser {
match event {
ParseEvent::Start(tag) => {
tag_stack.push((current_pos, tag));
}
ParseEvent::Text(txt) => {
builder.push(&txt);
current_pos += txt.len();
}
ParseEvent::End(end_tag) => {
let (start_off, tag) = tag_stack
.pop()
.expect("parser does not return unbalanced tags");
assert_eq!(end_tag, tag, "mismatched tags?");
add_attribute_for_tag(
&tag,
builder.add_attributes_for_range(start_off..current_pos),
);
if add_newline_after_tag(&tag) {
builder.push("\n\n");
current_pos += 2;
}
}
ParseEvent::Code(txt) => {
builder.push(&txt).font_family(FontFamily::MONOSPACE);
current_pos += txt.len();
}
ParseEvent::Html(txt) => {
builder
.push(&txt)
.font_family(FontFamily::MONOSPACE)
.text_color(BLOCKQUOTE_COLOR);
current_pos += txt.len();
}
ParseEvent::HardBreak => {
builder.push("\n\n");
current_pos += 2;
}
_ => (),
}
}
builder.build()
}
sourcepub fn push(&mut self, string: &str) -> AttributesAdder<'_>
pub fn push(&mut self, string: &str) -> AttributesAdder<'_>
Append a &str
to the end of the text.
This method returns a AttributesAdder
that can be used to style the newly
added string slice.
Examples found in repository?
examples/markdown_preview.rs (line 151)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
fn rebuild_rendered_text(text: &str) -> RichText {
let mut current_pos = 0;
let mut builder = RichTextBuilder::new();
let mut tag_stack = Vec::new();
let parser = Parser::new_ext(text, Options::ENABLE_STRIKETHROUGH);
for event in parser {
match event {
ParseEvent::Start(tag) => {
tag_stack.push((current_pos, tag));
}
ParseEvent::Text(txt) => {
builder.push(&txt);
current_pos += txt.len();
}
ParseEvent::End(end_tag) => {
let (start_off, tag) = tag_stack
.pop()
.expect("parser does not return unbalanced tags");
assert_eq!(end_tag, tag, "mismatched tags?");
add_attribute_for_tag(
&tag,
builder.add_attributes_for_range(start_off..current_pos),
);
if add_newline_after_tag(&tag) {
builder.push("\n\n");
current_pos += 2;
}
}
ParseEvent::Code(txt) => {
builder.push(&txt).font_family(FontFamily::MONOSPACE);
current_pos += txt.len();
}
ParseEvent::Html(txt) => {
builder
.push(&txt)
.font_family(FontFamily::MONOSPACE)
.text_color(BLOCKQUOTE_COLOR);
current_pos += txt.len();
}
ParseEvent::HardBreak => {
builder.push("\n\n");
current_pos += 2;
}
_ => (),
}
}
builder.build()
}
sourcepub fn add_attributes_for_range(
&mut self,
range: impl RangeBounds<usize>
) -> AttributesAdder<'_>
pub fn add_attributes_for_range( &mut self, range: impl RangeBounds<usize> ) -> AttributesAdder<'_>
Get an AttributesAdder
for the given range.
This can be used to modify styles for a given range after it has been added.
Examples found in repository?
examples/markdown_preview.rs (line 161)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
fn rebuild_rendered_text(text: &str) -> RichText {
let mut current_pos = 0;
let mut builder = RichTextBuilder::new();
let mut tag_stack = Vec::new();
let parser = Parser::new_ext(text, Options::ENABLE_STRIKETHROUGH);
for event in parser {
match event {
ParseEvent::Start(tag) => {
tag_stack.push((current_pos, tag));
}
ParseEvent::Text(txt) => {
builder.push(&txt);
current_pos += txt.len();
}
ParseEvent::End(end_tag) => {
let (start_off, tag) = tag_stack
.pop()
.expect("parser does not return unbalanced tags");
assert_eq!(end_tag, tag, "mismatched tags?");
add_attribute_for_tag(
&tag,
builder.add_attributes_for_range(start_off..current_pos),
);
if add_newline_after_tag(&tag) {
builder.push("\n\n");
current_pos += 2;
}
}
ParseEvent::Code(txt) => {
builder.push(&txt).font_family(FontFamily::MONOSPACE);
current_pos += txt.len();
}
ParseEvent::Html(txt) => {
builder
.push(&txt)
.font_family(FontFamily::MONOSPACE)
.text_color(BLOCKQUOTE_COLOR);
current_pos += txt.len();
}
ParseEvent::HardBreak => {
builder.push("\n\n");
current_pos += 2;
}
_ => (),
}
}
builder.build()
}
sourcepub fn build(self) -> RichText
pub fn build(self) -> RichText
Build the RichText
.
Examples found in repository?
examples/markdown_preview.rs (line 186)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
fn rebuild_rendered_text(text: &str) -> RichText {
let mut current_pos = 0;
let mut builder = RichTextBuilder::new();
let mut tag_stack = Vec::new();
let parser = Parser::new_ext(text, Options::ENABLE_STRIKETHROUGH);
for event in parser {
match event {
ParseEvent::Start(tag) => {
tag_stack.push((current_pos, tag));
}
ParseEvent::Text(txt) => {
builder.push(&txt);
current_pos += txt.len();
}
ParseEvent::End(end_tag) => {
let (start_off, tag) = tag_stack
.pop()
.expect("parser does not return unbalanced tags");
assert_eq!(end_tag, tag, "mismatched tags?");
add_attribute_for_tag(
&tag,
builder.add_attributes_for_range(start_off..current_pos),
);
if add_newline_after_tag(&tag) {
builder.push("\n\n");
current_pos += 2;
}
}
ParseEvent::Code(txt) => {
builder.push(&txt).font_family(FontFamily::MONOSPACE);
current_pos += txt.len();
}
ParseEvent::Html(txt) => {
builder
.push(&txt)
.font_family(FontFamily::MONOSPACE)
.text_color(BLOCKQUOTE_COLOR);
current_pos += txt.len();
}
ParseEvent::HardBreak => {
builder.push("\n\n");
current_pos += 2;
}
_ => (),
}
}
builder.build()
}
Trait Implementations§
source§impl Default for RichTextBuilder
impl Default for RichTextBuilder
source§fn default() -> RichTextBuilder
fn default() -> RichTextBuilder
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl !RefUnwindSafe for RichTextBuilder
impl !Send for RichTextBuilder
impl !Sync for RichTextBuilder
impl Unpin for RichTextBuilder
impl !UnwindSafe for RichTextBuilder
Blanket Implementations§
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> RoundFrom<T> for T
impl<T> RoundFrom<T> for T
§fn round_from(x: T) -> T
fn round_from(x: T) -> T
Performs the conversion.
§impl<T, U> RoundInto<U> for Twhere
U: RoundFrom<T>,
impl<T, U> RoundInto<U> for Twhere U: RoundFrom<T>,
§fn round_into(self) -> U
fn round_into(self) -> U
Performs the conversion.