0

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.

Hulabuli
  • 23
  • 5

2 Answers2

0

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>
Andrew KeepCoding
  • 7,040
  • 2
  • 14
  • 21
  • I've since learned a bit more, and feel like its worth mentioning for other that might need it. The the source: `ms-appx:///Assets/Fonts/BAUHS93.ttf` seems to be Irrelevant, and im my case can be removed. From further testing fonts seems to work as long as: `#"FONT-NAME"` is correct, and the required font is installed in the system, it will work. Its however important to get the true name of the font, as Rodrigo Balibrera pointed out, however. Sadly you can't rely on his method of finding the name, as some fonts use the wrong name in the top bar. – Hulabuli Sep 14 '22 at 13:31
0

Use pack://application option. However I've noted that you've used a colon instead of a semicolon after the word application. Check below:

<FontFamily x:Key="LatoThin">pack://application;,,,/Fonts/#Lato Thin</FontFamily>

Note that /Fonts/ refers to a folder in the solution where the font is stored.

Also note that the #Lato Thin part comes from the name on the font preview window.

enter image description here

  • This help a lot :) However I learned that in some cases this method won't work for finding the True name of the font. at least that's what i experienced. In this case i had to try my way to what the correct font name was. – Hulabuli Sep 14 '22 at 13:33