Skip to content

Commit

Permalink
Complete the basic RCTParagraphComponentAccessibilityProvider
Browse files Browse the repository at this point in the history
Summary:
Changelog:
[Internal] - add the first accessibilityElement

Reviewed By: shergin

Differential Revision: D22249310

fbshipit-source-id: e0f1920377eb9d7a22737cf7437b1c5bd7195c5d
  • Loading branch information
ZHUANGPP authored and facebook-github-bot committed Jul 6, 2020
1 parent 3d88249 commit 10a800d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@

- (instancetype)initWithString:(facebook::react::AttributedString)attributedString view:(UIView *)view;

/*
* Returns an array of `UIAccessibilityElement`s to be used for `UIAccessibilityContainer` implementation.
*/
- (NSArray<UIAccessibilityElement *> *)accessibilityElements;

/**
@abstract Array of accessibleElements for use in UIAccessibilityContainer implementation.
@abstract To make sure the provider is up to date.
*/
- (NSArray<UIAccessibilityElement *> *)accessibilityElements;
- (BOOL)isUpToDate:(facebook::react::AttributedString)currentAttributedString;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,34 @@ - (instancetype)initWithString:(AttributedString)attributedString view:(UIView *

- (NSArray<UIAccessibilityElement *> *)accessibilityElements
{
// verify if accessibleElements are exist
if (_accessibilityElements) {
return _accessibilityElements;
}
// build an array of the accessibleElements
NSMutableArray *elements = [NSMutableArray new];

NSString *accessibilityLabel = [_view valueForKey:@"accessibilityLabel"];
if (!accessibilityLabel.length) {
accessibilityLabel = RCTNSStringFromString(_attributedString.getString());
}
// add first element has the text for the whole textview in order to read out the whole text
UIAccessibilityElement *firstElement = [[UIAccessibilityElement alloc] initWithAccessibilityContainer:_view];
firstElement.isAccessibilityElement = YES;
firstElement.accessibilityTraits = UIAccessibilityTraitStaticText;
firstElement.accessibilityLabel = accessibilityLabel;
firstElement.accessibilityFrameInContainerSpace = _view.bounds;
[elements addObject:firstElement];

// add additional elements for those parts of text with embedded link so VoiceOver could specially recognize links

// add accessible element for truncation attributed string for automation purposes only
_accessibilityElements = [NSMutableArray new];
_accessibilityElements = elements;
return _accessibilityElements;
}

- (BOOL)isUpToDate:(facebook::react::AttributedString)currentAttributedString
{
return currentAttributedString == _attributedString;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#import "RCTParagraphComponentView.h"
#import "RCTParagraphComponentAccessibilityProvider.h"

#import <react/components/text/ParagraphComponentDescriptor.h>
#import <react/components/text/ParagraphProps.h>
Expand All @@ -26,6 +27,7 @@
@implementation RCTParagraphComponentView {
ParagraphShadowNode::ConcreteState::Shared _state;
ParagraphAttributes _paragraphAttributes;
RCTParagraphComponentAccessibilityProvider *_accessibilityProvider;
}

- (instancetype)initWithFrame:(CGRect)frame
Expand All @@ -35,7 +37,6 @@ - (instancetype)initWithFrame:(CGRect)frame
_props = defaultProps;

self.isAccessibilityElement = YES;
self.accessibilityTraits |= UIAccessibilityTraitStaticText;
self.opaque = NO;
self.contentMode = UIViewContentModeRedraw;
}
Expand Down Expand Up @@ -138,6 +139,23 @@ - (NSString *)accessibilityLabel
return RCTNSStringFromString(_state->getData().attributedString.getString());
}

- (NSArray *)accessibilityElements
{
if (![_accessibilityProvider isUpToDate:_state->getData().attributedString]) {
_accessibilityProvider =
[[RCTParagraphComponentAccessibilityProvider alloc] initWithString:_state->getData().attributedString
view:self];
}

self.isAccessibilityElement = NO;
return _accessibilityProvider.accessibilityElements;
}

- (UIAccessibilityTraits)accessibilityTraits
{
return [super accessibilityTraits] | UIAccessibilityTraitStaticText;
}

- (SharedTouchEventEmitter)touchEventEmitterAtPoint:(CGPoint)point
{
if (!_state) {
Expand Down

0 comments on commit 10a800d

Please sign in to comment.