aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPolesznyák Márk <contact@pml68.dev>2025-11-03 17:35:06 +0100
committerPolesznyák Márk <contact@pml68.dev>2025-11-03 17:35:06 +0100
commit0193d984c61dae797366e3438597caf0513e1a65 (patch)
tree6fd102f4ec6625a179a88f55e1a5c06250e704a5
parentfix: `Selection::text` not counting from `start.line` (diff)
downloadiced_selection-0193d984c61dae797366e3438597caf0513e1a65.tar.gz
feat: add `span!` macro (same as `text!`)
-rw-r--r--src/lib.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 3636bc7..34c1f73 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -86,6 +86,47 @@ macro_rules! rich_text {
);
}
+/// Creates a new [`Span`] of text with the provided content.
+///
+/// A [`Span`] is a fragment of some [`Rich`] text.
+///
+/// This macro uses the same syntax as [`format!`], but creates a new [`Span`] widget instead.
+///
+/// See [the formatting documentation in `std::fmt`](std::fmt)
+/// for details of the macro argument syntax.
+///
+/// # Example
+/// ```no_run,ignore
+/// use iced::font;
+/// use iced_selection::{rich_text, span};
+/// use iced::{color, never, Font};
+///
+/// #[derive(Debug, Clone)]
+/// enum Message {
+/// // ...
+/// }
+///
+/// fn view(state: &State) -> Element<'_, Message> {
+/// rich_text![
+/// span!("I am {}!", red).color(color!(0xff0000)),
+/// " ",
+/// span!("And I am bold!").font(Font { weight: font::Weight::Bold, ..Font::default() }),
+/// ]
+/// .on_link_click(never)
+/// .size(20)
+/// .into()
+/// }
+/// ```
+///
+/// [`Rich`]: crate::text::Rich
+/// [`Span`]: crate::text::Span
+#[macro_export]
+macro_rules! span {
+ ($($arg:tt)*) => {
+ $crate::text::Span::new(format!($($arg)*))
+ };
+}
+
/// Creates a new [`Text`] widget with the provided content.
///
/// # Example