Avalonia's FontManager
January 2, 2024 Avalonia Fonts ComboBoxThis post is about using Avalonia’s FontManager class.
Using Custom Fonts
There are a couple of samples demonstrating how to ship an app with a custom, embedded font (like a Google Font): Avalonia Blog Post and Avalonia Documentation.
I wasn’t able to find a sample showing how to create a simple font picker with the fonts installed on the system.
Creating a Font Picker
This is quite simple as it is using a standard ComboBox control which lists all the installed system fonts.
The View Model
|
|
The Items
collection is of type Avalonia.Media.FontFamily
and the FontManager
class from the same namespace provides convenient access to all the installed system fonts.
The View
The view is also quite simple. The item template of the ComboBox simply contains a TextBlock control which displays the name in the corresponding font.
|
|
If you only want to show the font names without rendering them in the corresponding font, simply remove line 6.
I tested this on Windows and Linux using Avalonia 11.0.6.