【问题标题】:Is it possible to set fixed sized fonts in Xamarin.Forms?是否可以在 Xamarin.Forms 中设置固定大小的字体?
【发布时间】:2019-08-23 16:33:25
【问题描述】:

我想知道是否可以(如果可以,如何)在 Xamarin.Forms 中设置固定大小的字体,使字体大小不受可访问性设置更改的影响。

假设我将标签中的字体大小设置为 16,如果我进入设备设置(Android 或 iOS)并将整体字体大小更改为更大或更小,我不希望该标签的大小发生变化.

我知道从可访问性的角度来看不建议这样做,但我有一个我真正需要的用例。

有可能吗?我该怎么做?

【问题讨论】:

    标签: xamarin mobile xamarin.forms xamarin.android xamarin.ios


    【解决方案1】:

    假设我将标签中的字体大小设置为 16,如果我进入设备设置(Android 或 iOS)并将整体字体大小更改为更大或更小,我不希望该标签的大小发生变化.

    关于在 Android 上禁用辅助功能字体缩放。您需要为标签创建自定义渲染器。

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

    在页面中使用这个自定义标签:

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

    此 MyLabel 字体大小不受辅助功能设置更改的影响。

    【讨论】:

      猜你喜欢
      • 2016-02-05
      • 2015-09-07
      • 2010-11-05
      • 2021-11-12
      • 2018-11-27
      • 2011-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多