How to load a Font.uasset and use it for FSlateFontInfo

I have a font /Content/Fonts/PT_Serif/PT-Serif_Bold_Font.uasset and I want to load it and use it for FSlateFontInfo.

Is this possible, or you can’t do it and you have to use the .ttf file for FSlateFontInfo?

I’m trying to set the font for a STextBlock. I made it work with the file format .ttf, but I want to use the .uasset file if it is possible.

I tried to use LoadObject<UFont>(nullptr, TEXT("/Game/Fonts/PT_Serif/PT-Serif_Bold_Font"));. but unfortunately you can’t use UFont for FSlateFontInfo.FontObject. Or maybe you can but you need to apply something to it?! Well that is why I need help with this. :slight_smile:

SNew(STextBlock)
				.ShadowColorAndOpacity(FLinearColor::Black)
				.ColorAndOpacity(FLinearColor::White)
				.ShadowOffset(FIntPoint(-1, 1))
				.Font(FSlateFontInfo(FPaths::ProjectContentDir() / TEXT("/Fonts/PT_Serif/PTSerif-Bold.ttf"), 40))
				.Text(FText::FromString("This is a test"))

Make sure the ttf file is in the PT_Serif folder.

SLoadingScreen.cpp (1.6 KB)

I wanted to use the .uasset file and not .ttf, and I found a way how to do it:

Inside .h file I added:

UObject* serifFont;
FSlateFontInfo info;

Then inside .cpp:

void SlateMessageWidget::Construct(const FArguments& InArgs, FString message, FString fontFamily, int fontSize) {
	serifFont = LoadObject<UObject>(nullptr, TEXT("/Game/Fonts/PT_Serif/PT-Serif"));
	info = FSlateFontInfo(serifFont, fontSize, FName(fontFamily));

And you can use “info” for the font:

.Font(info)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.