防止系统 FontSize 更改影响 Xamarin 应用程序中的大小 [英] Prevent system FontSize change from affecting the size in the Xamarin Application

查看:15
本文介绍了防止系统 FontSize 更改影响 Xamarin 应用程序中的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我注意到如果用户手机中的字体大小更改为中等以上,我制作的应用程序看起来会一团糟,我在谷歌上搜索了一下发现许多未回答或已回答但没有达到相同问题的程度.如果可能的话,我希望能够在 PCL 类中做到这一点,如果不可能,那么对我来说最有趣的平台是 android,因此可以针对 android 进行修复.这是我的 Xaml 代码示例,您可以参考:

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>

所以要清楚问题是如何防止系统覆盖我的字体大小,在这种情况下为 23?

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

谢谢

推荐答案

对于那些仍在努力如何在 Android 上禁用辅助功能字体缩放的人.您需要为标签、按钮和常见输入控件创建自定义渲染器,如下所示:

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);
        }
    }
}

对于 Xamarin 选择器控件,没有 FontSize 属性,因此我们可以将其添加到 App 类中:

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);

此外,通过更改此 NormalFontSize 属性,我们可以为选择器设置任何所需的字体大小,因为它在没有渲染器的情况下不可用.

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

这篇关于防止系统 FontSize 更改影响 Xamarin 应用程序中的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆