How Do I Support Accessibility Font Sizes in Xamarin Forms?

后端 未结 3 1715
故里飘歌
故里飘歌 2020-12-09 05:39

For example, I have a label on my page:

var label = new Label
{
    Text = \"Some text here.\",
    LineBreakMode = LineBreakMode.WordWrap,
    FontSize = De         


        
3条回答
  •  -上瘾入骨i
    2020-12-09 06:37

    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

    Results in:

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

提交回复
热议问题