UWP C# How can I get a list of the available font weights and styles for a font family?

SpectraCoder 141 Reputation points
2021-06-02T18:54:43.66+00:00

Currently my app gets a list of system fonts via Win2D's CanvasTextFormat.GetSystemFontFamilies(), but fonts like Arial Black are missing.

101843-font.jpg

This is probably because all Arial fonts variations are grouped together as one family. Only the default Arial regular font is available.

How can I get a list or array of the available font weights and styles for a font family?

Something similar is available in the Settings app:
101842-120332767-59882900-c2ef-11eb-9f5e-041465d15eab.jpg

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

Accepted answer
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,866 Reputation points
    2021-06-03T03:42:23.193+00:00

    Hello, Welcome to Micorosoft Q&A,

    How can I get a list of the available font weights and styles for a font family?

    Currently, there is no such api could load font weights and styles directly, derive from SharpDx.Direct2D1 api, you could get system font collection with GetSystemFontCollection method. then get each font where in above fontFamily. by chance Font class contain font weights and styles. For more please refer the following code.

    public void GetFontLibrary()  
    {  
        SharpDX.DirectWrite.Factory factory = new SharpDX.DirectWrite.Factory();  
        var fontCollection = factory.GetSystemFontCollection(false);  
        var familCount = fontCollection.FontFamilyCount;  
      
        for (int i = 0; i < familCount; i++)  
        {  
            var fontFamily = fontCollection.GetFontFamily(i);  
            var familyNames = fontFamily.FamilyNames;  
            var fontfacelist = new List<string>();  
            var fontface = new List<string>();  
            for (int j = 0; j < fontFamily.FontCount; j++)  
            {  
                var font = fontFamily.GetFont(j);  
                fontface.Add($"{font.Weight},{ font.Style}");  
                Debug.WriteLine($"{font.Weight},{ font.Style}");  
            }  
      
            int index;  
      
            if (!familyNames.FindLocaleName(CultureInfo.CurrentCulture.Name, out index))  
            {  
                if (!familyNames.FindLocaleName("en-us", out index))  
                {  
                    index = 0;  
                }  
            }  
      
            string name = familyNames.GetString(index);  
        }  
      
    }  
    

    Please note, that can't make sure the result were same as system setting page.


    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.

    1 person found this answer helpful.

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.