Skip to content

Commit

Permalink
Proper support of the accessibilityLabel for <Text> components on iOS
Browse files Browse the repository at this point in the history
Summary:
**PR changes**
The RCTText class originally overrode the accessibilityLabel and returned the raw text of the class ignoring if the accessibilityLabel was set explicitly in code.
Example:
  <Text accessibilityLabel="Example"> Hello World </Text> // returns "Hello World" instead of "Example" for the accessibility label

My update checks if the super's accessibilityLabel is not nil and returns the value else it returns the raw text itself as a default to mirror what a UIKit's UILabel does. The super's accessibilityLabel is nil if the accessibilityLabel is not ever set in code. I don't check the length of the label because if the value was set to an empty purposely then it will respect that and return whatever was set in code.
With the new changes:
  <Text accessibilityLabel="Example"> Hello World </Text> // returns "Example" for the accessibilityLabel

This change doesn't support nested <Text> components with both accessibilityLabel's value set respectively. The parent's value will return.
Example:

  // returns "Example" instead of "Example Test" for the accessibility label
  <Text accessibilityLabel="Example">
    Hello
    <Text accessibilityLabel="Test">
      World
    </Text>
  </Text>

The workaround is just to set the only the parent view's accessibilityLabel with the label desired for it and all its nested views or just not nest the views if possible.
I believe a bigger change would be needed to support accessibility for nested views, for now the changes I have made should satisfy the requirements.

Reviewed By: shergin

Differential Revision: D5806097

fbshipit-source-id: aef2d7cec4657317fcd7dd557448905e4b767f1a
  • Loading branch information
Joshua Alvarado authored and facebook-github-bot committed Sep 12, 2017
1 parent f6a188a commit 915a020
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Libraries/Text/RCTText.m
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ - (void)didMoveToWindow

- (NSString *)accessibilityLabel
{
NSString *superAccessibilityLabel = [super accessibilityLabel];
if (superAccessibilityLabel) {
return superAccessibilityLabel;
}
return _textStorage.string;
}

Expand Down
7 changes: 7 additions & 0 deletions RNTester/js/AccessibilityIOSExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class AccessibilityIOSExample extends React.Component<{}> {
Accessibility traits example
</Text>
</View>
<Text>
Text's accessibilityLabel is the raw text itself unless it is set explicitly.
</Text>
<Text accessibilityLabel="Test of accessibilityLabel"
accessible={true}>
This text component's accessibilityLabel is set explicitly.
</Text>
</View>
);
}
Expand Down

0 comments on commit 915a020

Please sign in to comment.