Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom font for all labels Xamarin Forms

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?

like image 455
Morten J Petersen Avatar asked Jun 14 '17 09:06

Morten J Petersen


1 Answers

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>
like image 63
Stephane Delcroix Avatar answered Sep 21 '22 05:09

Stephane Delcroix