Skip to main content

font_weights/
font_weights.rs

1//! This example demonstrates how to use font weights with text.
2
3use bevy::prelude::*;
4
5fn main() {
6    App::new()
7        .add_plugins(DefaultPlugins)
8        .add_systems(Startup, setup)
9        .run();
10}
11
12fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
13    let font: FontSource = asset_server.load("fonts/MonaSans-VariableFont.ttf").into();
14
15    commands.spawn(Camera2d);
16
17    commands.spawn((
18        Node {
19            flex_direction: FlexDirection::Column,
20            align_self: AlignSelf::Center,
21            justify_self: JustifySelf::Center,
22            align_items: AlignItems::Center,
23            ..default()
24        },
25        children![
26            (
27                Text::new("Font Weights"),
28                TextFont {
29                    font: font.clone(),
30                    font_size: FontSize::Px(32.0),
31                    ..default()
32                },
33                Underline,
34            ),
35            (
36                Node {
37                    flex_direction: FlexDirection::Column,
38                    padding: px(8.).all(),
39                    row_gap: px(8.),
40                    ..default()
41                },
42                Children::spawn(SpawnIter(
43                    [100, 134, 200, 300, 400, 500, 600, 700, 800, 900, 950]
44                        .into_iter()
45                        .map(move |weight| (
46                            Text(format!("Weight {weight}")),
47                            TextFont {
48                                font: font.clone(),
49                                font_size: FontSize::Px(32.0),
50                                weight: FontWeight(weight),
51                                ..default()
52                            },
53                        ))
54                ))
55            ),
56        ],
57    ));
58}