Search code examples
xamlfontsfont-familywinui-3

Winui 3, Unable to switch fonts


My current goal is to change the font-style according to my UX designers wishes. Ideally I want to change the default Font for the entire app. While attempting to update the default font, I noticed, that I was unable to update any font even, font family directly on a single textblock.

I've tried following method that this post describes. I do believe this will help med change the default font. It seems the font family setter itself is the problem. leavening me to believe I'm doing something wrong with the source.

Tried writing the source the following ways:

<FontFamily x:Key="Helvetica">"SolutionName":///"ProjectName"/Assets/HelveticaNeueRegular.ttf#Helvetica</FontFamily>

Also tried the following source, this works with multiple of my resource dictionaries:

<FontFamily x:Key="Helvetica">Assets/HelveticaNeueRegular.ttf</FontFamily>

And finally i've tried the following source method, this I might have done right as i don't completely understand it:

pack://application:,,,/MyAssembly;component/Fonts/#CustomFontName

For the sake of just making it work, and then cleaning later, I've placed these files in the main project. I've also tried both the buildactions None and Content

The way i tested if this works, to have a page with textblocks in a stackpanel writing out the alphabet, and using FontFamily={StaticResource Helvetica} with a stackpanel also referencing the font source directly.

Finally I've tried using other fonts to see if the font itself was the problem (did not work either), and also tried removing "Segoe UI" (The default winui 3 font) from windows, which proved not possible as it is a system font.

Any help is greatly appreciated.


Solution

  • Since I don't have the Helvetica font, I'm using another font that I found in my laptop.

    (In this case) Put your font file (.ttf/.ttc) in the Assets/Fonts folder and leave the property as it is (Content).

    Be careful about the font name. In this case, the file name is BAUHS93.ttf but the font name is Bauhaus 93. Try with BAUHS93.ttf (should be in you PC) first and then try Helvetica.

    App.xaml

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
                <!--  Other merged dictionaries here  -->
            </ResourceDictionary.MergedDictionaries>
            <!--  Other app resources here  -->
            <FontFamily x:Key="CustomFont">ms-appx:///Assets/Fonts/BAUHS93.ttf#Bauhaus 93</FontFamily>
            <Style TargetType="Button">
                <Setter Property="FontFamily" Value="{StaticResource CustomFont}" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>