diff options
author | Samantha Houts <samantha@teamredwall.com> | 2017-01-31 11:49:15 -0800 |
---|---|---|
committer | Kangho Hur <kangho.hur@samsung.com> | 2017-03-24 13:16:43 +0900 |
commit | 330f236a9e0c87b7e7ea2047e077b16df2e9b4d3 (patch) | |
tree | 4d33bfe5f705cdeb9b3c91f039805b976c35027f /Xamarin.Forms.Platform.iOS | |
parent | b362c49113b331280cc4f8c7e70fef3e52e07e37 (diff) | |
download | xamarin-forms-330f236a9e0c87b7e7ea2047e077b16df2e9b4d3.tar.gz xamarin-forms-330f236a9e0c87b7e7ea2047e077b16df2e9b4d3.tar.bz2 xamarin-forms-330f236a9e0c87b7e7ea2047e077b16df2e9b4d3.zip |
[All] Basic Accessibility Support (#713)
* [Core] Add accessibility properties
* [Controls] Add accessibility gallery
* [iOS] Implement accessibility properties
* [Android] Implement accessibilty properties
* [Win] Implement accessibility properties
* [Win] Select ListView item on selected for a11y
* Update docs
* TODO: macOS accessibility
* [iOS] Fix failing UI Tests
Diffstat (limited to 'Xamarin.Forms.Platform.iOS')
-rw-r--r-- | Xamarin.Forms.Platform.iOS/Renderers/ButtonRenderer.cs | 15 | ||||
-rw-r--r-- | Xamarin.Forms.Platform.iOS/Renderers/LabelRenderer.cs | 17 | ||||
-rw-r--r-- | Xamarin.Forms.Platform.iOS/ViewRenderer.cs | 57 | ||||
-rw-r--r-- | Xamarin.Forms.Platform.iOS/VisualElementRenderer.cs | 55 |
4 files changed, 142 insertions, 2 deletions
diff --git a/Xamarin.Forms.Platform.iOS/Renderers/ButtonRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/ButtonRenderer.cs index 68d4702d..3e99cd5d 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/ButtonRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/ButtonRenderer.cs @@ -86,6 +86,21 @@ namespace Xamarin.Forms.Platform.iOS UpdateImage(); } + protected override void SetAccessibilityLabel() + { + // If we have not specified an AccessibilityLabel and the AccessibiltyLabel is current bound to the Title, + // exit this method so we don't set the AccessibilityLabel value and break the binding. + // This may pose a problem for users who want to explicitly set the AccessibilityLabel to null, but this + // will prevent us from inadvertently breaking UI Tests that are using Query.Marked to get the dynamic Title + // of the Button. + + var elemValue = (string)Element?.GetValue(Accessibility.NameProperty); + if (string.IsNullOrWhiteSpace(elemValue) && Control?.AccessibilityLabel == Control?.Title(UIControlState.Normal)) + return; + + base.SetAccessibilityLabel(); + } + void OnButtonTouchUpInside(object sender, EventArgs eventArgs) { ((IButtonController)Element)?.SendClicked(); diff --git a/Xamarin.Forms.Platform.iOS/Renderers/LabelRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/LabelRenderer.cs index 445abf3e..82ceda81 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/LabelRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/LabelRenderer.cs @@ -137,6 +137,23 @@ namespace Xamarin.Forms.Platform.MacOS UpdateLineBreakMode(); } +#if __MOBILE__ + protected override void SetAccessibilityLabel() + { + // If we have not specified an AccessibilityLabel and the AccessibiltyLabel is current bound to the Text, + // exit this method so we don't set the AccessibilityLabel value and break the binding. + // This may pose a problem for users who want to explicitly set the AccessibilityLabel to null, but this + // will prevent us from inadvertently breaking UI Tests that are using Query.Marked to get the dynamic Text + // of the Label. + + var elemValue = (string)Element?.GetValue(Accessibility.NameProperty); + if (string.IsNullOrWhiteSpace(elemValue) && Control?.AccessibilityLabel == Control?.Text) + return; + + base.SetAccessibilityLabel(); + } +#endif + protected override void SetBackgroundColor(Color color) { #if __MOBILE__ diff --git a/Xamarin.Forms.Platform.iOS/ViewRenderer.cs b/Xamarin.Forms.Platform.iOS/ViewRenderer.cs index 01f8e06a..2cf9f754 100644 --- a/Xamarin.Forms.Platform.iOS/ViewRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/ViewRenderer.cs @@ -25,6 +25,11 @@ namespace Xamarin.Forms.Platform.MacOS public abstract class ViewRenderer<TView, TNativeView> : VisualElementRenderer<TView> where TView : View where TNativeView : NativeView { +#if __MOBILE__ + string _defaultAccessibilityLabel; + string _defaultAccessibilityHint; + bool? _defaultIsAccessibilityElement; +#endif NativeColor _defaultColor; public TNativeView Control { get; private set; } @@ -110,7 +115,59 @@ namespace Xamarin.Forms.Platform.MacOS base.OnRegisterEffect(effect); effect.Control = Control; } +#if __MOBILE__ + protected override void SetAccessibilityHint() + { + if (Control == null) + { + base.SetAccessibilityHint(); + return; + } + + if (Element == null) + return; + + if (_defaultAccessibilityHint == null) + _defaultAccessibilityHint = Control.AccessibilityHint; + + Control.AccessibilityHint = (string)Element.GetValue(Accessibility.HintProperty) ?? _defaultAccessibilityHint; + + } + + protected override void SetAccessibilityLabel() + { + if (Control == null) + { + base.SetAccessibilityLabel(); + return; + } + + if (Element == null) + return; + + if (_defaultAccessibilityLabel == null) + _defaultAccessibilityLabel = Control.AccessibilityLabel; + + Control.AccessibilityLabel = (string)Element.GetValue(Accessibility.NameProperty) ?? _defaultAccessibilityLabel; + } + + protected override void SetIsAccessibilityElement() + { + if (Control == null) + { + base.SetIsAccessibilityElement(); + return; + } + if (Element == null) + return; + + if (!_defaultIsAccessibilityElement.HasValue) + _defaultIsAccessibilityElement = Control.IsAccessibilityElement; + + Control.IsAccessibilityElement = (bool)((bool?)Element.GetValue(Accessibility.IsInAccessibleTreeProperty) ?? _defaultIsAccessibilityElement); + } +#endif protected override void SetAutomationId(string id) { if (Control == null) diff --git a/Xamarin.Forms.Platform.iOS/VisualElementRenderer.cs b/Xamarin.Forms.Platform.iOS/VisualElementRenderer.cs index 0e7af2a5..57c9ab12 100644 --- a/Xamarin.Forms.Platform.iOS/VisualElementRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/VisualElementRenderer.cs @@ -36,6 +36,11 @@ namespace Xamarin.Forms.Platform.MacOS readonly List<EventHandler<VisualElementChangedEventArgs>> _elementChangedHandlers = new List<EventHandler<VisualElementChangedEventArgs>>(); readonly PropertyChangedEventHandler _propertyChangedHandler; +#if __MOBILE__ + string _defaultAccessibilityLabel; + string _defaultAccessibilityHint; + bool? _defaultIsAccessibilityElement; +#endif EventTracker _events; VisualElementRendererFlags _flags = VisualElementRendererFlags.AutoPackage | VisualElementRendererFlags.AutoTrack; @@ -177,6 +182,7 @@ namespace Xamarin.Forms.Platform.MacOS } element.PropertyChanged += _propertyChangedHandler; + } OnElementChanged(new ElementChangedEventArgs<TElement>(oldElement, element)); @@ -188,6 +194,11 @@ namespace Xamarin.Forms.Platform.MacOS if (Element != null && !string.IsNullOrEmpty(Element.AutomationId)) SetAutomationId(Element.AutomationId); +#if __MOBILE__ + SetAccessibilityLabel(); + SetAccessibilityHint(); + SetIsAccessibilityElement(); +#endif } #if __MOBILE__ @@ -269,6 +280,12 @@ namespace Xamarin.Forms.Platform.MacOS #if __MOBILE__ else if (e.PropertyName == PlatformConfiguration.iOSSpecific.VisualElement.BlurEffectProperty.PropertyName) SetBlur((BlurEffectStyle)Element.GetValue(PlatformConfiguration.iOSSpecific.VisualElement.BlurEffectProperty)); + else if (e.PropertyName == Accessibility.HintProperty.PropertyName) + SetAccessibilityHint(); + else if (e.PropertyName == Accessibility.NameProperty.PropertyName) + SetAccessibilityLabel(); + else if (e.PropertyName == Accessibility.IsInAccessibleTreeProperty.PropertyName) + SetIsAccessibilityElement(); #endif } @@ -277,6 +294,40 @@ namespace Xamarin.Forms.Platform.MacOS effect.Container = this; } +#if __MOBILE__ + protected virtual void SetAccessibilityHint() + { + if (Element == null) + return; + + if (_defaultAccessibilityHint == null) + _defaultAccessibilityHint = AccessibilityHint; + + AccessibilityHint = (string)Element.GetValue(Accessibility.HintProperty) ?? _defaultAccessibilityHint; + } + + protected virtual void SetAccessibilityLabel() + { + if (Element == null) + return; + + if (_defaultAccessibilityLabel == null) + _defaultAccessibilityLabel = AccessibilityLabel; + + AccessibilityLabel = (string)Element.GetValue(Accessibility.NameProperty) ?? _defaultAccessibilityLabel; + } + + protected virtual void SetIsAccessibilityElement() + { + if (Element == null) + return; + + if (!_defaultIsAccessibilityElement.HasValue) + _defaultIsAccessibilityElement = IsAccessibilityElement; + + IsAccessibilityElement = (bool)((bool?)Element.GetValue(Accessibility.IsInAccessibleTreeProperty) ?? _defaultIsAccessibilityElement); + } +#endif protected virtual void SetAutomationId(string id) { AccessibilityIdentifier = id; @@ -286,11 +337,11 @@ namespace Xamarin.Forms.Platform.MacOS { if (color == Color.Default) #if __MOBILE__ - + BackgroundColor = _defaultColor; else BackgroundColor = color.ToUIColor(); - + #else Layer.BackgroundColor = _defaultColor.CGColor; else |