2

How to disable system font size scaling for maui app? android 33 version.

I tried to set configuration to default within MainActivity:

public override Android.Content.Res.Resources Resources
{
    get
    {
        Android.Content.Res.Configuration configuration = new Android.Content.Res.Configuration();
        configuration.SetToDefaults();

        DisplayMetrics metrics = new DisplayMetrics();

        if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.NMr1)
        {
            return CreateConfigurationContext(configuration).Resources;
        }
        else if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
        {
            base.Resources.UpdateConfiguration(configuration, metrics);
            return base.Resources;
        }
        else
        {
            return base.Resources;
        }
    }
}

in debug it resets display metrics density to default, but once in a while i see scaled font as system one. Also try tried to set:

configuration.FontScale = (float)1;

but the same.

1

2 Answers 2

5

You can simply use property

FontAutoScalingEnabled="False"

where in the label you want to.

Sign up to request clarification or add additional context in comments.

Comments

4

You can put the code below in the MainActivity.cs file. It can prevent to change the application font to match the system font.

 protected override void AttachBaseContext(Context @base)
     {
         Configuration configuration = new(@base.Resources.Configuration)
         {
             FontScale = 1.0f
         };
         ApplyOverrideConfiguration(configuration);
         base.AttachBaseContext(@base);
     }

3 Comments

thanks, it did the trick, but still need to set each ui element custom font size(dont use default or system fontSizes)
This does not work with named font sizes, such as Medium. Is there a way to make it work with those as well?
How would we do something similar for iOS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.