I’m currently deep in UMG/Slate with UE5. It’s been a few years since I’ve worked in Unreal regularly. And it’s also the first time I’m stepping into Slate proper.
I recently had the need to load up a font just for some custom styling on a slate widget I don’t have fully exposed in UMG. After some digging I got to this nugget of info:
NormalText = FTextBlockStyle()
.SetFont(FSlateFontInfo(FPaths::ProjectContentDir() / TEXT("UI/Fonts/MyFont.ttf"), 24));
The differences from the link is I’m using fonts in my project’s content folder via FPaths::ProjectContentDir()
.
Could it be cleaned up so I’m not spewing font paths throughout slate code? I think so! I found this post showing a handy macro that lets you load TTF fonts and hide away the details specific to your project.
My version:
#define TTF_FONT(RelativePath, ...) FSlateFontInfo(FPaths::ProjectContentDir() / "UI/Fonts/" RelativePath, __VA_ARGS__)
Using a FSlateStyleSet
I was able to use the macro as such and update my NormalText
definition to:
// void FMyCoreStyle::FStyle::Initialize() {
// Give it a friendly name. For example, whatever it's called in your Figma files.
Set("Body.B4",TTF_FONT("MyCoolFont/MyCoolFont-Regular.ttf", 10));
// more slate styling code ...
// Now refer to it via your friendly name.
NormalText = FTextBlockStyle()
.SetFont(GetFontStyle("Body.B4"));
The Set()
and GetFontStyle()
dance with using an FStyle
I lifted from browsing Engine/Source/Developer/TraceInsights/Private/Insights/InsightsStyle.h
and similar files in UE5’s source.
If there’s an easier way to handle fonts in slate I’d love to hear about it.
Happy Coding!