Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How do you get the default font (family) for a platform/app?

I've been searching around and tried out a few different things, none of which seem to work, but I just want to find out if there's a way to get the name of the default font on the current platform?

I ask because I've got some info pages which display in a WebView (because they have some external links which are easiest handled via a WebView rather than some kind of formatted label) but they don't seem to use the system/platform default font (iOS doesn't, not yet tested Android). My plan was to add CSS to the pages so that the font, at least, looks the same as the rest of the app.

Here's what I've tried:
Xamarin.Forms.Font.SystemFontOfSize(10).FontFamily
Xamarin.Forms.Font.SystemFontOfSize(NamedSize.Small).FontFamily
Xamarin.Forms.Font.Default.FontFamily

Unfortunately, on the iOS simulator, they all just return null/empty. I've not tried on other platforms yet as I figure there's no point - I need a cross-platform solution (so if it doesn't work on one platform then it's no good).

Most of the posts that come up when I search are all about setting a default/app-wide font. I have no desire to do that. The only thing I can find that comes close to answering my question is this post from SO ( stackoverflow.com/questions/44088536/whats-the-default-font-family ) - is what Ada says correct? Would I really have to implement native-services, and use dependency-injection to get the default font value? Or is there a more straight-forward way to do this?

Thanks in advance.

Answers

  • DrTJ2066DrTJ2066 USMember ✭✭

    I've solved this by adding a DependencyService and generating a new control to see what is the default font name.
    For example in iOS you'll be able to achieve this as following:
    var fontFamily = new UIKit.UITextField().Font.Name;

  • djgtramdjgtram HUMember ✭✭
    edited July 2018

    UWP solution that might go into that DependencyService:

    new Windows.Globalization.Fonts.LanguageFontGroup(CultureInfo.CurrentUICulture.Name).ModernDocumentFont.FontFamily

Sign In or Register to comment.