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

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

问题描述

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

var 标签 = 新标签{Text = "这里有一些文字.",LineBreakMode = LineBreakMode.WordWrap,FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))};

如何根据用户对字体大小的无障碍设置来增加(或减少)此标签的字体大小?例如,在 iOS 中,您可以在设置">通用">辅助功能">大文本"下为您的设备设置字体大小.我相信 Apple 将其称为动态文本",几乎是您的应用必须支持的一项要求.

这同样适用于我应用中的其他控件(按钮、条目等).

我已在 iPhone 上尝试过此设置,但它似乎并没有改变我的应用程序中的所有内容.TableView 部分标题和列表视图单元格等一些内容正在发生变化,但我的标准标签和条目等内容没有发生变化.

解决方案

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

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

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

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

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

示例渲染器:

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

[程序集:ExportRenderer(typeof(BodyLabel), typeof(iOSLabelBodyRenderer))]命名空间 Foobar.iOS{公共类 iOSLabelBodyRenderer : LabelRenderer{公共 iOSLabelBodyRenderer() { }protected override void OnElementChanged(ElementChangedEventArgs

结果:

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

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

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

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.

解决方案

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.

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.

There are six Dynamic font types defined in iOS 9:

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

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.

Example Renderer:

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

Results in:

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天全站免登陆