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 I set the font family of a label, platform to platform.

I'm a beginner in Xamarin and xml. I know a some C#. I'm trying to set the font of my Xamarin Forms application on the android version specifically, that way I can get a monospace font (Droid Sans Mono). I've looked around at a lot of threads about fonts and OnPlatform and data bindings, but I'm a little bit lost. I'm pretty sure Font.FontFamily is read-only as well.

here are some examples of things I have tried:
Label1.FontFamily = Device.OnPlatform("MarkerFelt-Thin", "Droid Sans Mono", "Comic Sans MS");

<Label x:Name="Label1" Text="" HorizontalOptions="Center" VerticalOptions="CenterAndExpand"> <Label.FontFamily> <OnPlatform x:TypeArguments="String"> <On Platform="Android">Droid Sans Mono</On> </OnPlatform> </Label.FontFamily> </Label>

Best Answers

  • Othello7Othello7 Member ✭✭
    Accepted Answer

    Ok! It was pretty much that i had tried too many alternate ways to set the font and after some commenting out of code they did, this worked. And others like it.
    <Label.FontFamily> <OnPlatform x:TypeArguments="x:String"> <On Platform="iOS" Value="CourierNewPSMT" /> <On Platform="Android" Value="monospace" /> <On Platform="UWP" Value="Courier New" /> </OnPlatform> </Label.FontFamily>
    Also, using the custom font worked.

    Thank you to everyone on these forums and to microsoft support for helping me with my problem.

Answers

  • NMackayNMackay GBInsider, University admin
    edited December 2019

    Can you not use x:OnPlatform markup extension?

    FontFamily="{x:OnPlatform Android='Droid Sans Mono', iOS='Something Else', UWP='XXX'}"
    

    in a style you could set it like this

        <OnPlatform x:Key="semiBoldFont" x:TypeArguments="x:String">
            <On Platform="iOS" Value="FontName-SemiBold" />
            <On Platform="Android" Value="Fonts/FontName-SemiBold.otf#Font SemiBold" />
            <On Platform="UWP" Value="/Assets/Fonts/FontName-SemiBold.ttf#Font" />
        </OnPlatform>
    

    And then just set your label style etc in your theme

  • JohnHardmanJohnHardman GBUniversity admin

    @Othello7 said:
    I'm a beginner in Xamarin and xml. I know a some C#. I'm trying to set the font of my Xamarin Forms application on the android version specifically, that way I can get a monospace font (Droid Sans Mono).

    On Android, to get a fixed-width font, try "monospace" rather than "Droid Sans Mono" when setting the FontFamily property.

    @Othello7 said:
    I'm pretty sure Font.FontFamily is read-only as well.

    It isn't.

    One of the first things I did when I started working with Xamarin.Forms was to create a page on which I could select from a list of font families, toggle a bold button, toggle an italics button, and have a Label update to reflect those settings. I still have that page and it allows me to change the FontFamily as many times as I want, with the change being reflected in the Label. I obtain the list of font families from a DependencyService that I implemented, with each platform providing a different list of font families (I've been suggesting to Xamarin that they provide some fonts that work cross-platform, but so far to no avail).

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited December 2019

    To use custom fonts in Xamarin.Forms (Droid project):

    • Place in your Assets folder, and ensure the build type is AndroidAsset.
    • Define the fonts for each OS.

      <ResourceDictionary>
          <OnPlatform x:TypeArguments="x:String" x:Key="BoldFont">
              <On Platform="Android" Value="OpenSans-Bold.ttf#Open Sans" />
              <On Platform="UWP" Value="/Assets/OpenSans-Bold.ttf#Open Sans" />
              <On Platform="iOS" Value="OpenSans-Bold" />
          </OnPlatform>
      </ResourceDictionary>
      
    • Reference the key use the font .

      <StackLayout>
          <Label Text="Welcome to Xamarin Forms! (OpenSans-Bold)" FontFamily="{StaticResource BoldFont}" />
          <Label Text="Welcome to Xamarin Forms! (Default)" />
      </StackLayout>
      

    Tutorials about using custom fonts you can refer to:
    https://xamarinhelp.com/custom-fonts-xamarin-forms/
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/fonts#use-a-custom-font

  • Othello7Othello7 Member ✭✭

    Thanks for the help, but I still cant seem to make it work.

    I tried NMackay's solution. I had not heard of:

    FontFamily="{x:OnPlatform Android='Droid Sans Mono', iOS='Something Else', UWP='XXX'}"
    

    I don't really need the other platforms just yet, I am just trying to make it work on android
    But It didn't seem to work for me.
    So then I took JohnHardman's advice:

    FontFamily="{x:OnPlatform Android='monospace', iOS='Something Else', UWP='XXX'}" 
    

    But it still came up with the default font.

    I have already tried things along the lines of this (from NMackay):

        <OnPlatform x:Key="semiBoldFont" x:TypeArguments="x:String">
            <On Platform="iOS" Value="FontName-SemiBold" />
            <On Platform="Android" Value="Fonts/FontName-SemiBold.otf#Font SemiBold" />
            <On Platform="UWP" Value="/Assets/Fonts/FontName-SemiBold.ttf#Font" />
        </OnPlatform>
    

    I had before also tried Jarvan's suggestion of using a custom font, but it didn't do anything the way I did it then.
    But I saw the code was a little different so I tried it again and got an exception:
    System.InvalidOperationException: Cannot determine property to provide the value for.

    So I really don't know what to do. I felt like I was banging my head against a wall, going around, looking at a lot of forums, trying everything. It is great to be able to get help from people who know what they're doing.

  • JohnHardmanJohnHardman GBUniversity admin

    @Othello7

    Try:

                <Label Text="This is the XAML syntax for specifying FontFamily"
                       LineBreakMode="WordWrap">
                    <Label.FontFamily>
                        <OnPlatform x:TypeArguments="x:String">
                            <On Platform="iOS" Value="CourierNewPSMT" />
                            <On Platform="Android" Value="monospace" />
                            <On Platform="UWP" Value="Courier New" />
                        </OnPlatform>
                    </Label.FontFamily>
                </Label>
    
  • JarvanJarvan Member, Xamarin Team Xamurai

    Here is the basic sample file which works fine on my side, you can refer to it.

  • Othello7Othello7 Member ✭✭

    I tried it, but it did this:

    I tried debugging anyway and it wasn't monospace.
    I'm using Visual Studio 2017, USB debugging on a KitKat 4.4 tablet if that is any help.

  • Othello7Othello7 Member ✭✭

    @Jarvan said:
    Here is the basic sample file which works fine on my side, you can refer to it.

    I tried running it, it didn't work on my end. I got this exception:

    Unhandled Exception:
    
    Android.Content.Res.Resources+NotFoundException: File res/drawable/abc_vector_test.xml from drawable resource ID #0x7f070059
    

    it was on line 22 in MainActivity.cs
    base.OnCreate(savedInstanceState);

  • JarvanJarvan Member, Xamarin Team Xamurai

    Unhandled Exception: Android.Content.Res.Resources+NotFoundException

    Android 5.0 (API 21) is recommended as the minimum API, lower versions will cause full compatibility of the Android support libraries. Try to deploy the code on SDK 5 or higher.

  • JohnHardmanJohnHardman GBUniversity admin

    The Minimum API wouldn't prevent monospace working for a Label on Android 4.4 (it might cause other problems though). I've just tested on 4.2.2 running on a Huawei phone and it worked fine. The Minimum API should be taken into account when targeting devices, but it I wouldn't expect it to be causing a problem with FontFamily on Android.

  • Othello7Othello7 Member ✭✭

    I would like for my apps to be able to work on my old tablet, KitKat 4.4, API 19. I guess if I'm forced I can switch to newer, but if its at all possible I would use the older version.
    It seems not to recognize x:TypeArguments for me, and have trouble with the On Platform stuff sometimes (none of which have actually worked). Originally I was just going to make an android xamarin app. But I discovered that there wasn't much beginners tutorials for the android specific project. So I switched to a xamarin cross-platform project which also might be beneficial later on.

    My computer does not recognize my android hardware, So I don't know what model tablet I am using.

  • Othello7Othello7 Member ✭✭

    My setting in the App.Android look just like the image in the microsoft info @NMackay sent

    Everything else is mostly default settings.

  • NMackayNMackay GBInsider, University admin

    Kitkat doesn't support SSL etc (well without iffy hacks), it's not secure. if your app is for non store I'd understand

  • Othello7Othello7 Member ✭✭
    Accepted Answer

    Ok! It was pretty much that i had tried too many alternate ways to set the font and after some commenting out of code they did, this worked. And others like it.
    <Label.FontFamily> <OnPlatform x:TypeArguments="x:String"> <On Platform="iOS" Value="CourierNewPSMT" /> <On Platform="Android" Value="monospace" /> <On Platform="UWP" Value="Courier New" /> </OnPlatform> </Label.FontFamily>
    Also, using the custom font worked.

    Thank you to everyone on these forums and to microsoft support for helping me with my problem.

Sign In or Register to comment.