Skip to content

Commit

Permalink
Introduces a new 'shouldCalculateAccessibilityLabelPerLink' property …
Browse files Browse the repository at this point in the history
…on NIAttributedLabel that allows setting custom accessibility labels. This property defaults to YES.

PiperOrigin-RevId: 327479892
  • Loading branch information
Nobody authored and material-automation committed Aug 19, 2020
1 parent 407b47c commit 5387ace
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/attributedlabel/src/NIAttributedLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ extern NSString* const NIAttributedLabelLinkAttributeName; // Value is an NSText
@property (nonatomic) NSTextCheckingType dataDetectorTypes; // Default: NSTextCheckingTypeLink
@property (nonatomic) BOOL deferLinkDetection; // Default: NO

/**
* Determines whether each link's accessibilityLabel will be determined by the label's text.
* Defaults to YES.
*
* If enabled, each link will dynamically determine its accessibilityLabel based on the text to
* which the link is attached.
*
* If disabled, each link will be set to either self.accessibilityLabel or
* self.accessibilityAttributedLabel.
*/
@property(nonatomic) BOOL shouldCalculateAccessibilityLabelPerLink;

- (void)addLink:(NSURL *)urlLink range:(NSRange)range;
- (void)removeAllExplicitLinks; // Removes all links that were added by addLink:range:. Does not remove autodetected links.

Expand Down
25 changes: 22 additions & 3 deletions src/attributedlabel/src/NIAttributedLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ - (void)_configureDefaults {
self.linkColor = NIIsTintColorGloballySupported() ? self.tintColor : [UIColor blueColor];
self.dataDetectorTypes = NSTextCheckingTypeLink;
self.highlightedLinkBackgroundColor = [UIColor colorWithWhite:0.5f alpha:0.5f];
self.shouldCalculateAccessibilityLabelPerLink = YES;
}

- (id)initWithFrame:(CGRect)frame {
Expand Down Expand Up @@ -1129,7 +1130,9 @@ - (NIViewAccessibilityElement *)accessibilityElementForRange:(NSRange)range {
[[NIViewAccessibilityElement alloc] initWithAccessibilityContainer:self
frameInContainer:bounds
pointsInContainer:pointsArray];
element.accessibilityLabel = [self.mutableAttributedString.string substringWithRange:range];
NSString *accessibilityLabel = [self.mutableAttributedString.string substringWithRange:range];
[self updateAccessibilityLabelOnElement:element withAccessibilityLabel:accessibilityLabel];

// Set the frame to fallback on if |element|'s accessibility container is changed externally.
CGRect rectValueInWindowCoordinates = [self convertRect:bounds toView:nil];
CGRect rectValueInScreenCoordinates =
Expand Down Expand Up @@ -1785,6 +1788,19 @@ - (void)invalidateAccessibleElements {
self.accessibleElements = nil;
}

- (void)updateAccessibilityLabelOnElement:(NIViewAccessibilityElement *)element
withAccessibilityLabel:(NSString *)accessibilityLabel {
if (_shouldCalculateAccessibilityLabelPerLink) {
element.accessibilityLabel = accessibilityLabel;
return;
}

if (@available(iOS 11, *)) {
element.accessibilityAttributedLabel = self.accessibilityAttributedLabel;
}
element.accessibilityLabel = self.accessibilityLabel;
}

- (NSArray *)accessibleElements {
if (nil != _accessibleElements) {
return _accessibleElements;
Expand Down Expand Up @@ -1822,7 +1838,8 @@ - (NSArray *)accessibleElements {
NIViewAccessibilityElement *element = [[NIViewAccessibilityElement alloc]
initWithAccessibilityContainer:self
frameInContainer:rectValue.CGRectValue];
element.accessibilityLabel = label;
[self updateAccessibilityLabelOnElement:element withAccessibilityLabel:label];

// Set the frame to fallback on if |element|'s accessibility container is changed
// externally.
CGRect rectValueInWindowCoordinates = [self convertRect:rectValue.CGRectValue toView:nil];
Expand All @@ -1838,7 +1855,9 @@ - (NSArray *)accessibleElements {
NIViewAccessibilityElement *element =
[[NIViewAccessibilityElement alloc] initWithAccessibilityContainer:self
frameInContainer:self.bounds];
element.accessibilityLabel = self.attributedText.string;
[self updateAccessibilityLabelOnElement:element
withAccessibilityLabel:self.attributedText.string];

// Set the frame to fallback on if |element|'s accessibility container is changed externally.
CGRect boundsInWindowCoordinates = [self convertRect:self.bounds toView:nil];
CGRect boundsInScreenCoordinates =
Expand Down

0 comments on commit 5387ace

Please sign in to comment.