unintelligible
unintelligible

Reputation: 113

How to override Android's system font?

So, in a nutshell, I try setting the Label.FontFamily property and it doesn't take effect.

I have an extremely simple requirement for a mobile app. The Android version of the app being developed should always render text in the Roboto font, regardless of what the font family in the system settings is.

This is sample markup that doesn't work:

<Label VerticalOptions="CenterAndExpand"
       HorizontalOptions="CenterAndExpand"
       TextColor="White"
       HorizontalTextAlignment="Center"
       FontSize="46"
       FontFamily="Roboto">Hello world!</Label>

I've set the system font to a weird looking one called Travel that came pre-installed with my phone. Everything on my device is rendered in that font, unfortunately that includes the greeting label, whose code is posted here.

I tried Roboto.ttf instead of simply Roboto. I tried changing the MainTheme

<style name="MainTheme" parent ="Theme.AppCompat.Light.NoActionBar">
    <item name="android:textAllCaps">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:fontFamily">Roboto</item>
    <item name="android:windowFullscreen">true</item>
  </style>

I looked throughout the solution for the words "font" and "family", unfortunately, nothing relevant comes up.

For the record, I know I'll have to use an idiom eventually, but I wanted to get the change to work in a quick-and-dirty way first.

How can I override that font from system settings?

Upvotes: 0

Views: 373

Answers (1)

AbbyWang - MSFT
AbbyWang - MSFT

Reputation: 660

You can override the Android's system font by the following steps:

1, Copy the font file to the Assets folder, in my demo, I used Roboto-Italic.ttf to make it clear.

2, Set the fontfamily of the label to the font you have added to assets:

    <Label.FontFamily>
        <OnPlatform x:TypeArguments="x:String">
            <On Platform="Android" Value="Roboto-Italic.ttf#Roboto-Italic.ttf" />
         </OnPlatform>
    </Label.FontFamily>

Upvotes: 1

Related Questions