Loading a font with multiple files?

Heiko 1,291 Reputation points
2020-10-08T11:21:46.083+00:00

It is possible to load a font from the LocalState directory:

textBlock.FontFamily = new FontFamily("ms-appdata:///Local/angsa.ttf#Angsana New");

But what if there are multiple files for the font:

angsa.ttf (normal)
angsai.ttf (italic)
angsab.ttf (bold)

The user should only see 'Angsana New' once in the font selection. If he chooses bold, italic or normal, does the programmer have to guess/know which font file to load?

Or is there a way to load all font files for the font with a single call? Unfortunately the following does not work:

textBlock.FontFamily = new FontFamily("ms-appdata:///Local/angsai.ttf,ms-appdata:///Local/angsa.ttf,ms-appdata:///Local/angsab.ttf#Angsana New");

Developer technologies | Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Richard Zhang-MSFT 6,936 Reputation points Microsoft Employee Moderator
    2020-10-08T13:04:43.433+00:00

    Hello,

    Welcome to Microsoft Q&A.

    For system fonts which installed on the device (such as Segoe UI), a font family contains multiple fonts with different weights, which will be switched when calling TextBlock.FontWeight.

    For custom fonts, the application will calculate based on the current font-weight instead of switching to another font.

    This means that the text weights displayed by the following two TextBlocks may be different (even if the font files are in the same directory):

       <TextBlock FontFamily="ms-appdata:///Local/angsa.ttf#Angsana New" FontWeight="Bold" Text="Hello World"/>  
       <TextBlock FontFamily="ms-appdata:///Local/angsab.ttf#Angsana New" Text="Hello World"/>  
    

    Therefore, for custom fonts, it is recommended to distinguish them by file, and different font-weight refer to different font files.

    In addition, FontFamily.Source can be a path, but we cannot use , to separate multiple paths.

    Thanks.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.