forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 2
/
RCTParagraphComponentAccessibilityProvider.mm
181 lines (162 loc) · 9.06 KB
/
RCTParagraphComponentAccessibilityProvider.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTParagraphComponentAccessibilityProvider.h"
#import <Foundation/Foundation.h>
#import <react/renderer/components/text/ParagraphProps.h>
#import <react/renderer/textlayoutmanager/RCTAttributedTextUtils.h>
#import <react/renderer/textlayoutmanager/RCTTextLayoutManager.h>
#import <react/renderer/textlayoutmanager/TextLayoutManager.h>
#import "RCTAccessibilityElement.h"
#import "RCTConversions.h"
#import "RCTFabricComponentsPlugins.h"
#import "RCTLocalizationProvider.h"
using namespace facebook::react;
@implementation RCTParagraphComponentAccessibilityProvider {
NSMutableArray<UIAccessibilityElement *> *_accessibilityElements;
AttributedString _attributedString;
RCTTextLayoutManager *_layoutManager;
ParagraphAttributes _paragraphAttributes;
CGRect _frame;
__weak UIView *_view;
}
- (instancetype)initWithString:(facebook::react::AttributedString)attributedString
layoutManager:(RCTTextLayoutManager *)layoutManager
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
frame:(CGRect)frame
view:(UIView *)view
{
if (self = [super init]) {
_attributedString = attributedString;
_layoutManager = layoutManager;
_paragraphAttributes = paragraphAttributes;
_frame = frame;
_view = view;
}
return self;
}
- (NSArray<UIAccessibilityElement *> *)accessibilityElements
{
if (_accessibilityElements) {
return _accessibilityElements;
}
__block NSInteger numberOfLinks = 0;
__block NSInteger numberOfButtons = 0;
__block NSString *truncatedText;
// build an array of the accessibleElements
NSMutableArray<UIAccessibilityElement *> *elements = [NSMutableArray new];
NSString *accessibilityLabel = _view.accessibilityLabel;
if (accessibilityLabel.length == 0) {
accessibilityLabel = RCTNSStringFromString(_attributedString.getString());
}
// add first element has the text for the whole textview in order to read out the whole text
RCTAccessibilityElement *firstElement =
[[RCTAccessibilityElement alloc] initWithAccessibilityContainer:_view.superview];
firstElement.isAccessibilityElement = YES;
firstElement.accessibilityTraits = _view.accessibilityTraits;
firstElement.accessibilityLabel = accessibilityLabel;
firstElement.accessibilityLanguage = _view.accessibilityLanguage;
firstElement.accessibilityFrame = UIAccessibilityConvertFrameToScreenCoordinates(_view.bounds, _view);
[firstElement setAccessibilityActivationPoint:CGPointMake(
firstElement.accessibilityFrame.origin.x + 1.0,
firstElement.accessibilityFrame.origin.y + 1.0)];
[elements addObject:firstElement];
// add additional elements for those parts of text with embedded link so VoiceOver could specially recognize links
[_layoutManager getRectWithAttributedString:_attributedString
paragraphAttributes:_paragraphAttributes
enumerateAttribute:RCTTextAttributesAccessibilityRoleAttributeName
frame:_frame
usingBlock:^(CGRect fragmentRect, NSString *_Nonnull fragmentText, NSString *value) {
if ([fragmentText isEqualToString:firstElement.accessibilityLabel]) {
// The fragment is the entire paragraph. This is handled as `firstElement`.
return;
}
if ((![value isEqualToString:@"button"] && ![value isEqualToString:@"link"])) {
return;
}
if ([value isEqualToString:@"button"] &&
([fragmentText isEqualToString:@"See Less"] ||
[fragmentText isEqualToString:@"See More"])) {
truncatedText = fragmentText;
return;
}
RCTAccessibilityElement *element =
[[RCTAccessibilityElement alloc] initWithAccessibilityContainer:self->_view];
element.isAccessibilityElement = YES;
if ([value isEqualToString:@"link"]) {
element.accessibilityTraits = UIAccessibilityTraitLink;
numberOfLinks++;
} else if ([value isEqualToString:@"button"]) {
element.accessibilityTraits = UIAccessibilityTraitButton;
numberOfButtons++;
}
element.accessibilityLabel = fragmentText;
element.frame = fragmentRect;
[elements addObject:element];
}];
if (numberOfLinks > 0 || numberOfButtons > 0) {
__block NSInteger indexOfLink = 1;
__block NSInteger indexOfButton = 1;
[elements enumerateObjectsUsingBlock:^(UIAccessibilityElement *element, NSUInteger idx, BOOL *_Nonnull stop) {
if (idx == 0) {
return;
}
if (element.accessibilityTraits & UIAccessibilityTraitLink) {
NSString *test = [RCTLocalizationProvider RCTLocalizedString:@"Link %ld of %ld."
withDescription:@"index of the link"];
element.accessibilityHint = [NSString stringWithFormat:test, (long)indexOfLink++, (long)numberOfLinks];
} else {
element.accessibilityHint =
[NSString stringWithFormat:[RCTLocalizationProvider RCTLocalizedString:@"Button %ld of %ld."
withDescription:@"index of the button"],
(long)indexOfButton++,
(long)numberOfButtons];
}
}];
}
if (numberOfLinks > 0 && numberOfButtons > 0) {
firstElement.accessibilityHint =
[RCTLocalizationProvider RCTLocalizedString:@"Links and buttons are found, swipe to move to them."
withDescription:@"accessibility hint for links and buttons inside text"];
} else if (numberOfLinks > 0) {
NSString *firstElementHint = (numberOfLinks == 1)
? [RCTLocalizationProvider RCTLocalizedString:@"One link found, swipe to move to the link."
withDescription:@"accessibility hint for one link inside text"]
: [NSString stringWithFormat:[RCTLocalizationProvider
RCTLocalizedString:@"%ld links found, swipe to move to the first link."
withDescription:@"accessibility hint for multiple links inside text"],
(long)numberOfLinks];
firstElement.accessibilityHint = firstElementHint;
} else if (numberOfButtons > 0) {
NSString *firstElementHint = (numberOfButtons == 1)
? [RCTLocalizationProvider RCTLocalizedString:@"One button found, swipe to move to the button."
withDescription:@"accessibility hint for one button inside text"]
: [NSString stringWithFormat:[RCTLocalizationProvider
RCTLocalizedString:@"%ld buttons found, swipe to move to the first button."
withDescription:@"accessibility hint for multiple buttons inside text"],
(long)numberOfButtons];
firstElement.accessibilityHint = firstElementHint;
}
if (truncatedText && truncatedText.length > 0) {
firstElement.accessibilityHint = (numberOfLinks > 0 || numberOfButtons > 0)
? [NSString
stringWithFormat:@"%@ %@",
firstElement.accessibilityHint,
[RCTLocalizationProvider
RCTLocalizedString:[NSString stringWithFormat:@"Double tap to %@.", truncatedText]
withDescription:@"accessibility hint for truncated text with links or buttons"]]
: [RCTLocalizationProvider RCTLocalizedString:[NSString stringWithFormat:@"Double tap to %@.", truncatedText]
withDescription:@"accessibility hint for truncated text"];
}
// add accessible element for truncation attributed string for automation purposes only
_accessibilityElements = elements;
return _accessibilityElements;
}
- (BOOL)isUpToDate:(facebook::react::AttributedString)currentAttributedString
{
return currentAttributedString == _attributedString;
}
@end