diff --git a/Cargo.lock b/Cargo.lock index 1e2ba61a..e55307e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -850,6 +850,13 @@ dependencies = [ "eframe", ] +[[package]] +name = "custom_font_style" +version = "0.1.0" +dependencies = [ + "eframe", +] + [[package]] name = "custom_window_frame" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index bf7e74b5..1aad261e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ members = [ "examples/custom_3d_glow", "examples/custom_3d_three-d", "examples/custom_font", + "examples/custom_font_style", "examples/custom_window_frame", "examples/download_image", "examples/file_dialog", diff --git a/egui/src/style.rs b/egui/src/style.rs index 84632df9..1d0c40e8 100644 --- a/egui/src/style.rs +++ b/egui/src/style.rs @@ -145,6 +145,32 @@ pub struct Style { /// The [`FontFamily`] and size you want to use for a specific [`TextStyle`]. /// /// The most convenient way to look something up in this is to use [`TextStyle::resolve`]. + /// + /// If you would like to overwrite app text_styles + /// + /// ``` + /// # let mut ctx = egui::Context::default(); + /// use egui::FontFamily::Proportional; + /// use egui::FontId; + /// use egui::TextStyle::*; + /// + /// // Get current context style + /// let mut style = (*ctx.style()).clone(); + /// + /// // Redefine text_styles + /// style.text_styles = [ + /// (Heading, FontId::new(30.0, Proportional)), + /// (Name("Heading2".into()), FontId::new(25.0, Proportional)), + /// (Name("Context".into()), FontId::new(23.0, Proportional)), + /// (Body, FontId::new(18.0, Proportional)), + /// (Monospace, FontId::new(14.0, Proportional)), + /// (Button, FontId::new(14.0, Proportional)), + /// (Small, FontId::new(10.0, Proportional)), + /// ].into(); + /// + /// // Mutate global style with above changes + /// ctx.set_style(style); + /// ``` pub text_styles: BTreeMap, /// If set, labels buttons wtc will use this to determine whether or not diff --git a/examples/custom_font_style/Cargo.toml b/examples/custom_font_style/Cargo.toml new file mode 100644 index 00000000..a3b7169f --- /dev/null +++ b/examples/custom_font_style/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "custom_font_style" +version = "0.1.0" +authors = ["tami5 "] +license = "MIT OR Apache-2.0" +edition = "2021" +rust-version = "1.60" +publish = false + + +[dependencies] +eframe = { path = "../../eframe" } diff --git a/examples/custom_font_style/README.md b/examples/custom_font_style/README.md new file mode 100644 index 00000000..d337602c --- /dev/null +++ b/examples/custom_font_style/README.md @@ -0,0 +1,3 @@ +```sh +cargo run -p custom_font_style +``` diff --git a/examples/custom_font_style/src/main.rs b/examples/custom_font_style/src/main.rs new file mode 100644 index 00000000..b702489f --- /dev/null +++ b/examples/custom_font_style/src/main.rs @@ -0,0 +1,72 @@ +#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release + +use eframe::egui; +use egui::{FontFamily, FontId, RichText, TextStyle}; + +#[inline] +fn heading2() -> TextStyle { + TextStyle::Name("Heading2".into()) +} + +#[inline] +fn heading3() -> TextStyle { + TextStyle::Name("ContextHeading".into()) +} + +fn configure_text_styles(ctx: &egui::Context) { + use FontFamily::Proportional; + use TextStyle::*; + + let mut style = (*ctx.style()).clone(); + style.text_styles = [ + (Heading, FontId::new(30.0, Proportional)), + (heading2(), FontId::new(25.0, Proportional)), + (heading3(), FontId::new(23.0, Proportional)), + (Body, FontId::new(18.0, Proportional)), + (Monospace, FontId::new(14.0, Proportional)), + (Button, FontId::new(14.0, Proportional)), + (Small, FontId::new(10.0, Proportional)), + ] + .into(); + ctx.set_style(style); +} + +fn content(ui: &mut egui::Ui) { + ui.heading("Top Heading"); + ui.add_space(5.); + ui.label(LOREM_IPSUM); + ui.add_space(15.); + ui.label(RichText::new("Sub Heading").text_style(heading2()).strong()); + ui.label(LOREM_IPSUM); + ui.add_space(15.); + ui.label(RichText::new("Context").text_style(heading3()).strong()); + ui.add_space(5.); + ui.label(LOREM_IPSUM); +} + +struct MyApp; + +impl MyApp { + fn new(cc: &eframe::CreationContext<'_>) -> Self { + configure_text_styles(&cc.egui_ctx); + Self + } +} + +impl eframe::App for MyApp { + fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { + egui::CentralPanel::default().show(ctx, content); + } +} + +fn main() { + let options = eframe::NativeOptions::default(); + + eframe::run_native( + "egui example: global font style", + options, + Box::new(|cc| Box::new(MyApp::new(cc))), + ); +} + +pub const LOREM_IPSUM: &str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";