Skip to content

Commit 14d9f6e

Browse files
authored
Add a from_line_height constructor function to TextFont. (#20335)
# Objective `TextFont` has `from_font` and `from_font_size` constructors but is missing one for line height. ## Solution * Added a `from_line_height` constructor function to `TextFont`. * Also added `From<Handle<Font>>` and `From<LineHeight>` impls. We could also add a `From<f32>` for font size but I thought it might be confusing. At some some point we want to add responsive text size support, can do it then.
1 parent 8487a0f commit 14d9f6e

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

crates/bevy_text/src/text.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bevy_color::Color;
44
use bevy_derive::{Deref, DerefMut};
55
use bevy_ecs::{prelude::*, reflect::ReflectComponent};
66
use bevy_reflect::prelude::*;
7-
use bevy_utils::once;
7+
use bevy_utils::{default, once};
88
use cosmic_text::{Buffer, Metrics};
99
use serde::{Deserialize, Serialize};
1010
use smallvec::SmallVec;
@@ -315,6 +315,11 @@ impl TextFont {
315315
Self::default().with_font_size(font_size)
316316
}
317317

318+
/// Returns a new [`TextFont`] with the specified line height.
319+
pub fn from_line_height(line_height: LineHeight) -> Self {
320+
Self::default().with_line_height(line_height)
321+
}
322+
318323
/// Returns this [`TextFont`] with the specified font face handle.
319324
pub fn with_font(mut self, font: Handle<Font>) -> Self {
320325
self.font = font;
@@ -340,6 +345,21 @@ impl TextFont {
340345
}
341346
}
342347

348+
impl From<Handle<Font>> for TextFont {
349+
fn from(font: Handle<Font>) -> Self {
350+
Self { font, ..default() }
351+
}
352+
}
353+
354+
impl From<LineHeight> for TextFont {
355+
fn from(line_height: LineHeight) -> Self {
356+
Self {
357+
line_height,
358+
..default()
359+
}
360+
}
361+
}
362+
343363
impl Default for TextFont {
344364
fn default() -> Self {
345365
Self {

0 commit comments

Comments
 (0)