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

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

问题描述

所以我注意到,如果将用户电话中的字体大小更改为中等以上,我制作的应用程序看起来会很混乱,我在Google上搜索了一下,发现许多未答复或未回答,但没有达到相同的问题. 我希望能够在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>

所以要明确的问题是,如何防止系统覆盖我的fontsize(在这种情况下为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天全站免登陆