Prevent system FontSize change from affecting the size in the Xamarin Application
Asked Answered
M

3

3

So I noticed that an App I made will look messed up if the Font size in the users phone is changed to above medium, I googled a bit found many unanswered or answered but not to the point of the same question. I want to be able to do that in the PCL class if possible , if not possible then the most interesting platform for me is android so a fix specific for android would do. Here is a sample of my Xaml code so you can get a reference:

 <Label  Text="STORE" FontSize="23" HeightRequest="40" WidthRequest="212" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"></Label>

So to be clear the question is how do I prevent the system from overriding my fontsize which is 23 in this case?

Thanks

Mailemailed answered 8/4, 2017 at 6:3 Comment(0)
S
4

SImply add this in your MainActivity after OnCreate.

        #region Font size change Prevent
        public override Resources Resources
        {
            get
            {
                var config = new Configuration();
                config.SetToDefaults();
                return CreateConfigurationContext(config).Resources;
            }
        }
        #endregion Font size change Prevent
Siouan answered 27/1, 2021 at 4:33 Comment(3)
Had an issue with font size overflowing dialogs when the user set the font size too large. This solved the issue by resetting the FontScale to 1 as a default.Precious
This doenst work for anyone who is using app recourses folder in pcl. (cause same namespace) just go with #50290263Monopetalous
Thank you! This solved my issue! Font size is now respecting the assigned valueTemptress
U
3

For those who still struggle how to disable accessibility font scaling on Android. You need to create custom renderer for label, button and common input controls like this:

using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(Label), typeof(MyApp.Droid.Renderers.LabelRendererDroid))]
namespace MyApp.Droid.Renderers
{
    class LabelRendererDroid : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement == null) return;
            Control.SetTextSize(Android.Util.ComplexUnitType.Dip, (float)e.NewElement.FontSize);
        }
    }
}

For Xamarin picker controls there is no FontSize property, so we can add it to App class:

public static double NormalFontSize => Device.GetNamedSize(NamedSize.Medium, typeof(Picker));

and then utilize it in picker renderer:

Control.SetTextSize(Android.Util.ComplexUnitType.Dip, (float)App.NormalFontSize);

Also by changing this NormalFontSize property we can set whatever desired font size for picker, as it is not available without renderer.

Usurer answered 25/1, 2018 at 9:24 Comment(1)
Gary McGhee builds out the exact custom renderer classes for both Labels and Buttons here. Additionally, BrianLK gives the method that performs the lion's share of the work for Entries later down the thread. Good luck!Tameika
S
0

Have you seen the items in chapter 5 of this book? https://developer.xamarin.com/guides/xamarin-forms/creating-mobile-apps-xamarin-forms/

Although not an exact answer, depending on what you are attempting to do you might be able to use the example at the end of the chapter modified to allow for a "Max" font size. All from within the PCL.

It's worth noting however that the inability to scale the font to a big size could be an indication of accessibility problem.

Sephira answered 8/4, 2017 at 8:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.