5
votes

I have downloaded a font from fonts.google.com and would like to set the font family for all labels in my Xamarin Forms app.

Adding the following code to all labels seems like an excessive amount of work

                <Label.FontFamily>
                        <OnPlatform x:TypeArguments="x:String">
                        <OnPlatform.Android>Mogra-Regular.ttf#Mogra-Regular</OnPlatform.Android>
                    </OnPlatform>
                </Label.FontFamily>

Ideally I would set the font family in the Resource Dictionary where I've already succesfully set text colour and font attributes, however, I've been unsuccesful in doing so.

I have managed to change the font family using a custom renderer but the moment I alter the label text colour or font attributes using dynamic resources the font family is reset to default.

Is there any way that will allow me to universally use my custom font and still be allowed to dynamically change other aspects of any given label?

1
we derived from Label and made a custom control so that we could uniform design accross the app.Digitalsa1nt

1 Answers

6
votes

Create an implicit Style (a Style without an x:Key) in your App.xaml and it'll be applied to all Labels

<Style TargetType="Label">
  <Setter Property="FontFamily">
    <Setter.Value>
      <OnPlatform x:TypeArguments="x:String">
        <OnPlatform.Android>Mogra-Regular.ttf#Mogra-Regular</OnPlatform.Android>
      </OnPlatform>
    </Setter.Value>
  </Setter>
</Style>