Prevent system FontSize change from affecting the size in the Xamarin Application

吃可爱长大的小学妹 提交于 2019-12-01 08:44:58

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!