rusteguieframe

Rust Egui, how do i set the color of a heading?


I am trying to create a heading in my Rust egui code and set its color to white, but I am unsure of how to do so. My code currently includes a function render_date that uses the colored_label function to display a date string on the screen in white text. However, I would like this text to also be a heading.

Here is my current render_date function:

pub fn render_date(view: &mut InitView, ui: &mut eframe::egui::Ui) {
    let date = view.format_date_string();
    ui.vertical_centered(|ui|{
        ui.add_space(50.0);
        ui.horizontal(|ui|{
            ui.add_space(50.0);
            ui.colored_label(egui::Color32::from_rgb(255, 255, 255), date);
        });
    });
}

it has to be a heading or else my styling will now work. Is there a way to wrap a colored_label, with a heading?


Solution

  • Don't use the egui::Ui::heading shortcut, explicitly instantiate an egui::RichText and use that instead:

    ui.label(egui::RichText::new(date).heading().color(egui::Color32::from_rgb(255, 255, 255)));
    

    This way you can set all the styling options yourself.