adamasan
adamasan

Reputation: 1182

Is it possible to set fixed sized fonts in Xamarin.Forms?

I would like to know if it is possible (and if so, how) to set fixed size fonts in Xamarin.Forms in a way that the font size isn't affected by the changes in accessibility settings.

Let's say I set a font size in a label to 16, I don't want the size of that label to change if I go into the device settings (Android or iOS) and change the overall font size to larger or smaller.

I am aware that this isn't recommended from a accessibility point of view, but I have a use case where I really need that.

Is it possible? How can I do that?

Upvotes: 1

Views: 1423

Answers (1)

Cherry Bu - MSFT
Cherry Bu - MSFT

Reputation: 10346

Let's say I set a font size in a label to 16, I don't want the size of that label to change if I go into the device settings (Android or iOS) and change the overall font size to larger or smaller.

About disable accessibility font scaling on Android. You need to create custom renderer for label.

MyLabelRender:

[assembly: ExportRenderer(typeof(MyLabel), typeof(MyLabelRender))]
namespace Demo1.Droid
{
class MyLabelRender: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);

    }
}
}

Using this custom label in page:

 <local:MyLabel
            FontSize="15"
            HorizontalOptions="CenterAndExpand"
            Text="Welcome to Xamarin.Forms!1111111111111"
            VerticalOptions="CenterAndExpand" />

This MyLabel font size isn't affected by the changes in accessibility settings.

Upvotes: 3

Related Questions