如何支持Xamarin表单中的辅助功能字体大小? [英] How Do I Support Accessibility Font Sizes in Xamarin Forms?

查看:129
本文介绍了如何支持Xamarin表单中的辅助功能字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我的页面上有一个标签:

For example, I have a label on my page:

var label = new Label
{
    Text = "Some text here.",
    LineBreakMode = LineBreakMode.WordWrap,
    FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))
};

如何根据用户对字体大小的可访问性设置,使该标签的字体大小增加(或减少)?例如,在iOS中,您可以在设置>常规>可访问性>较大文本下为设备设置字体大小.我相信Apple将此称为动态文本",几乎是您的应用程序支持所必需的.

How do I make this label's font size increase (or decrease) depending on the user's accessibility settings for font sizes? For example, in iOS you can set the Font Size for your device under Settings > General > Accessibility > Larger Text. I believe that Apple calls this "Dynamic Text" and is almost a requirement for your app to support.

我的应用程序中的其他控件(按钮,条目等)也是如此.

The same applies for other controls in my app (buttons, entrys, etc).

我已经在iPhone上尝试了此设置,但它似乎并没有改变我应用程序中的所有内容.诸如TableView节标题和列表视图单元格之类的东西正在发生变化,但是诸如我的标准Labels和Entrys之类的事物却没有变化.

I have tried this setting on my iPhone and it does not appear to be changing all things in my app. There are a few things like TableView section headers and list view cells that are changing, but things like my standard Labels and Entrys are not.

推荐答案

您需要提供从preferredFontWithTextStyle(C#= UIFont.PreferredFontForTextStyle)返回的UIFont作为标签,按钮,条目,等等... Xamarin.Forms不会知道.

You would need to supply the UIFont returned from preferredFontWithTextStyle (C# = UIFont.PreferredFontForTextStyle) as your usage context of a label, button, entry, etc... would not be known to Xamarin.Forms.

因此,我为一个客户端执行的操作是将基本渲染器和视图元素子类化,并向这些元素添加仅iOS的属性,以便它们可以定义如何开始在UI中使用该控件以及由此由iOS渲染的上下文这些控件将受动态文本大小调整的限制.

So what I did for one client was subclass the base renderers and view elements and add an iOS-only property to those elements so they could define the context of how that control is begin used in the UI and thus when rendered by iOS these controls will be subject to Dynamic Text sizing.

iOS 9中定义了六种动态字体类型:

There are six Dynamic font types defined in iOS 9:

  • UICTFontTextStyleBody
  • UICTFontTextStyleCaption1
  • UICTFontTextStyleCaption2
  • UICTFontTextStyleFootnote
  • UICTFontTextStyleHeadline
  • UICTFontTextStyleSubhead

注意:Xamarin.iOS没有像Swift那样为它们定义常量/枚举(ObjC也没有定义它们),因此它们作为NSString传递,请参见下面的示例.

Note: Xamarin.iOS does not have constants/enum defined for these like Swift does (ObjC does not define these either), so they are passed as a NSString, see example below.

为名为BodyLabel的标签子类设置UICTFontTextStyleBody:

Sets UICTFontTextStyleBody for a label subclass called BodyLabel:

[assembly: ExportRenderer(typeof(BodyLabel), typeof(iOSLabelBodyRenderer))]
namespace Foobar.iOS
{
    public class iOSLabelBodyRenderer : LabelRenderer
    {
        public iOSLabelBodyRenderer() { }

        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
                Control.Font = UIFont.GetPreferredFontForTextStyle(new NSString("UICTFontTextStyleBody"));
        }
    }
}

结果:

注意:从技术上讲,您还应该实现UIContentSizeCategoryDidChangeNotification通知,以便在用户更改动态字体大小时调整控件的大小/使它们无效.

Note: Technically you should also implement UIContentSizeCategoryDidChangeNotification notifications so you resize/invalidate your controls when the user changes the dynamic font size.

这篇关于如何支持Xamarin表单中的辅助功能字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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