1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Add extra_text_line_spacing to control vertical spacing between text lines (#8040)

Add `extra_text_line_spacing` to control vertical spacing between text
lines

**Description**
This PR adds a new `Spacing::extra_text_line_spacing` field to control
additional vertical spacing between lines of text.

The spacing is applied to text layout by adjusting
`TextFormat::line_height` based on the font row height plus the
configured extra spacing.

This improves text readability and allows consistent line spacing
customization for widgets such as `TextEdit` and `Label`.

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
rustbasic
2026-08-04 17:06:47 +09:00
committed by GitHub
parent dae9adf307
commit 5c0b690dab
3 changed files with 25 additions and 4 deletions

View File

@@ -419,6 +419,9 @@ pub struct Spacing {
/// Default width of a [`crate::TextEdit`].
pub text_edit_width: f32,
/// Additional vertical spacing between lines of text.
pub extra_text_line_spacing: f32,
/// Checkboxes, radio button and collapsing headers have an icon at the start.
/// This is the width/height of the outer part of this icon (e.g. the BOX of the checkbox).
pub icon_width: f32,
@@ -1458,6 +1461,7 @@ impl Default for Spacing {
slider_rail_height: 8.0,
combo_width: 100.0,
text_edit_width: 280.0,
extra_text_line_spacing: 0.0,
icon_width: 14.0,
icon_width_inner: 8.0,
icon_spacing: 4.0,
@@ -1948,6 +1952,7 @@ impl Spacing {
slider_rail_height,
combo_width,
text_edit_width,
extra_text_line_spacing,
icon_width,
icon_width_inner,
icon_spacing,
@@ -2014,6 +2019,10 @@ impl Spacing {
ui.add(DragValue::new(text_edit_width).range(0.0..=1000.0));
ui.end_row();
ui.label("Extra text line spacing");
ui.add(DragValue::new(extra_text_line_spacing).range(0.0..=20.0));
ui.end_row();
ui.label("Tooltip wrap width");
ui.add(DragValue::new(tooltip_width).range(0.0..=1000.0));
ui.end_row();

View File

@@ -769,14 +769,19 @@ impl WidgetText {
.visuals
.override_text_color
.unwrap_or(crate::Color32::PLACEHOLDER);
// We want the style overrides to take precedence over the fallback font
let font_id = FontSelection::default().resolve_with_fallback(style, fallback_font);
let line_height = ctx
.fonts_mut(|f| f.row_height(&font_id) + style.spacing.extra_text_line_spacing);
let mut layout_job = LayoutJob::simple_format(
text,
TextFormat {
// We want the style overrides to take precedence over the fallback font
font_id: FontSelection::default()
.resolve_with_fallback(style, fallback_font),
font_id,
color,
valign: default_valign,
line_height: Some(line_height),
..Default::default()
},
);

View File

@@ -471,6 +471,8 @@ impl TextEdit<'_> {
let font_id = font_selection.resolve(ui.style());
let row_height = ui.fonts_mut(|f| f.row_height(&font_id));
let line_height = row_height + ui.spacing().extra_text_line_spacing;
const MIN_WIDTH: f32 = 24.0; // Never make a [`TextEdit`] more narrow than this.
let available_width = ui.available_width().at_least(MIN_WIDTH);
let desired_width = desired_width
@@ -489,12 +491,17 @@ impl TextEdit<'_> {
layout_job.halign = align.x();
// We want to keep the trailing whitespace, since hiding it feels really weird when typing
layout_job.keep_trailing_whitespace = true;
for section in &mut layout_job.sections {
section.format.line_height = Some(line_height);
}
ui.fonts_mut(|f| f.layout_job(layout_job))
};
let layouter = layouter.unwrap_or(&mut default_layouter);
let min_inner_height = (desired_height_rows.at_least(1) as f32) * row_height;
let min_inner_height = (desired_height_rows.at_least(1) as f32) * line_height;
let id = id.unwrap_or_else(|| {
if let Some(id_salt) = id_salt {