From 0193d984c61dae797366e3438597caf0513e1a65 Mon Sep 17 00:00:00 2001 From: Polesznyák Márk Date: Mon, 3 Nov 2025 17:35:06 +0100 Subject: feat: add `span!` macro (same as `text!`) --- src/lib.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) 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 -- cgit v1.2.3