Replies: 3 comments 3 replies
-
ok, just figured it out now, this really isn't intuitive without docs explaining it: so you need to construct a new BTreeMap from scratch, give this a name and a vector with your font in the new function. pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
// This is also where you can customize the look and feel of egui using
// `cc.egui_ctx.set_visuals` and `cc.egui_ctx.set_fonts`.
let mut fonts = FontDefinitions::default();
fonts.font_data.insert(
"B612Mono-Bold".to_owned(),
FontData::from_static(include_bytes!("../assets/fonts/B612Mono-Bold.ttf")),
);
let mut newfam = BTreeMap::new();
newfam.insert(
FontFamily::Name("B612Mono-Bold".into()),
vec!["B612Mono-Bold".to_owned()],
);
fonts.families.append(&mut newfam);
cc.egui_ctx.set_fonts(fonts);
// Load previous app state (if any).
// Note that you must enable the `persistence` feature for this to work.
if let Some(storage) = cc.storage {
return eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default();
}
Default::default()
} the you can reference this family: ui.label(
egui::RichText::new(format!("{:>10.4}", self.curr_meas.to_string()))
.color(egui::Color32::YELLOW)
.size(60.0)
.font(FontId {
size: 60.0,
family: FontFamily::Name("B612Mono-Bold".into()),
}),
); This is really a clunky wway to do it instead of having the ability to just pick a font where the whole hierarchical family thing isn't needed. It makes a lot of sense to have this for the ui in general as it makes font preferences etc easier, but when trying to build e.g. a measurement UI where you ship the specific font in the package the current approach complicates things a lot. @emilk would you say it would be a reasonable request to be able to select fonts directly without the need to wrap and reference them through families? Or at the minimum: add such an example to the docs? |
Beta Was this translation helpful? Give feedback.
-
Made a macro for myself probly not the best way to do it but maybe it helps use egui::FontData;
use egui::FontDefinitions;
use egui::FontFamily;
macro_rules! load_font {
($ctx: expr, $($name:literal, $path:literal as $font_family:expr),*) => {
let mut fonts = FontDefinitions::default();
$(
fonts.font_data.insert($name.into(), FontData::from_static(include_bytes!($path)));
fonts.families.get_mut(&$font_family).unwrap().push($name.into());
)*
$ctx.set_fonts(fonts);
};
}
fn load_fonts(ctx: &egui::Context) {
load_font!(
ctx,
"japanese_fallback",
"resources/fonts/NotoSansJP-Regular.ttf" as FontFamily::Proportional
);
load_font!(
ctx,
"korean_fallback",
"resources/fonts/NotoSansKR-Regular.ttf" as FontFamily::Proportional
);
load_font!(
ctx,
"symbols_fallback",
"resources/fonts/NotoSansSymbols2-Regular.ttf" as FontFamily::Proportional
);
} |
Beta Was this translation helpful? Give feedback.
-
Hi, I've been trying for about 2 hours to set a custom font to a single RichText label.
I DO NOT want to override Proportional or Monospace fonts for the whole app, unfortunately that is all the documentation talks about or I am too stupid to find it.
I even tried to create a custom font family, but then when using it it always tells me there are no fonts bound to it although I believe that is exactly what I did.
Any help on this? I'm baffled that it is so difficult to change the font on a single label.
I do not even need a new fmaily for my pupose. Let me load the ttf I'm shipping with the app and set it somewhere...
Beta Was this translation helpful? Give feedback.
All reactions