How to get font file from windows phone

106 views Asked by At

I'm creating library in Xamarin platform and I need to get device (android,iOS and windows) font file. In android I can get the font files from/system/fonts folder.

Android:

string[] fontfiles = System.IO.Directory.GetFiles("/system/fonts");

Can anyone share your idea to get font file from the Windows device?.

Thanks in Advance.

Sasi.

1

There are 1 answers

0
York Shen On

How to get font file from windows phone

You can't get system font file in Windows Phone, but you can get the system font. Refer to ptallett's blog, do the following steps :

  1. In order to get system font in UWP, we must use DirectX, install these NuGet pacakges into your project :

    • SharpDX
    • SharpDX.Direct2D1
  2. GetFontFiles() will return all system fonts.

    public class GetFont : FontDemo.IGetFont
    {
        public string[] GetFontFiles()
        {
            var fontList = new List<string>();
            var factory = new SharpDX.DirectWrite.Factory();
            var fontCollection = factory.GetSystemFontCollection(false);
            var familyCount = fontCollection.FontFamilyCount;
    
            for (int i = 0; i < familyCount; i++)
            {
                var fontFamily = fontCollection.GetFontFamily(i);
                var familyNames = fontFamily.FamilyNames;
                int index;
    
                if (!familyNames.FindLocaleName(CultureInfo.CurrentCulture.Name, out index))
                {
                    if (!familyNames.FindLocaleName("en-us", out index))
                    {
                        index = 0;
                    }
                }
                string name = familyNames.GetString(index);
                fontList.Add(name);
            }
            return fontList.ToArray();
        }
    }
    
  3. Using DependencyService in your PCL to get the result :

    string[] fontfiles = DependencyService.Get<IGetFont>().GetFontFiles();