Bug 41735 - Make Xamarin.Forms.Platform.Android.FontExtensions.Typefaces public
Summary: Make Xamarin.Forms.Platform.Android.FontExtensions.Typefaces public
Status: RESOLVED FEATURE
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.2.0
Hardware: PC Windows
: Low enhancement
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-06-11 19:30 UTC by Dominik Weber
Modified: 2016-06-15 17:17 UTC (History)
2 users (show)

Tags: enhancement
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FEATURE

Description Dominik Weber 2016-06-11 19:30:38 UTC
Currently it's not possible to include custom fonts on android without writing renderers.

I'm using the following code to register my own fonts in the typefaces cache:

        private void RegisterFonts(string path)
        {
            if (path == null)
                throw new ArgumentNullException(nameof(path));

            var typefaces = (Dictionary<Tuple<string, FontAttributes>, Typeface>)typeof(FontExtensions).GetField("Typefaces", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);

            foreach (var font in Assets.List(path))
            {
                var fontFile = System.IO.Path.Combine(path, font);
                var fontName = System.IO.Path.GetFileNameWithoutExtension(font);
                typefaces[new Tuple<string, FontAttributes>(fontName, FontAttributes.None)] = Typeface.CreateFromAsset(Assets, fontFile);
            }
        }

By doing so I don't have to write custom renderers but can specify the FontFamily just like on iOS. However this code relies on reflection to gain access to the private Typefaces field.
Please make this field public or add a method RegisterFont(string name, Typeface typeface).
Comment 1 Samantha Houts [MSFT] 2016-06-15 17:17:03 UTC
A discussion for this particular enhancement is currently on the Forms Development mailing list. Please continue this discussion there so that we have one place to go for all of the relevant details. 

http://lists.ximian.com/pipermail/forms-devel/2016-June/000056.html

Thank you!!