Skip to content

Commit

Permalink
[TabBarView] Add accessibility label (#7727)
Browse files Browse the repository at this point in the history
This adds the `accessibilityLabel` to MDCTabBarView, this falls back to the titleLabel.text if the accessibility label isn't set. This has been tested on a device.

## Testing
Steps to reproduce
1. Update the [example](https://github.com/material-components/material-components-ios/blob/develop/components/Tabs/examples/MDCTabBarViewTypicalExampleViewController.m) to have a custom `accessibilityLabel` for one of the tabBarItems
```objc
item1.accessibilityLabel = @"Hello world";
```
2. Select the item with Voiceover on
3. Navigate to the other items with titles but no custom value.
  • Loading branch information
codeman7 committed Jun 27, 2019
1 parent 3fa4749 commit 1d36b07
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions components/Tabs/src/TabBarView/MDCTabBarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

static NSString *const kImageKeyPath = @"image";
static NSString *const kTitleKeyPath = @"title";
static NSString *const kAccessibilityLabelKeyPath = @"accessibilityLabel";

@interface MDCTabBarView ()

Expand Down Expand Up @@ -92,6 +93,7 @@ - (void)setItems:(NSArray<UITabBarItem *> *)items {
MDCTabBarViewItemView *itemView = [[MDCTabBarViewItemView alloc] init];
itemView.translatesAutoresizingMaskIntoConstraints = NO;
itemView.titleLabel.text = item.title;
itemView.accessibilityLabel = item.accessibilityLabel;
itemView.titleLabel.textColor = [self titleColorForState:UIControlStateNormal];
itemView.iconImageView.image = item.image;
[itemView setContentCompressionResistancePriority:UILayoutPriorityRequired
Expand Down Expand Up @@ -226,13 +228,20 @@ - (void)addObserversToTabBarItems {
forKeyPath:kTitleKeyPath
options:NSKeyValueObservingOptionNew
context:kKVOContextMDCTabBarView];
[item addObserver:self
forKeyPath:kAccessibilityLabelKeyPath
options:NSKeyValueObservingOptionNew
context:kKVOContextMDCTabBarView];
}
}

- (void)removeObserversFromTabBarItems {
for (UITabBarItem *item in self.items) {
[item removeObserver:self forKeyPath:kImageKeyPath context:kKVOContextMDCTabBarView];
[item removeObserver:self forKeyPath:kTitleKeyPath context:kKVOContextMDCTabBarView];
[item removeObserver:self
forKeyPath:kAccessibilityLabelKeyPath
context:kKVOContextMDCTabBarView];
}
}

Expand All @@ -259,6 +268,8 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
tabBarItemView.iconImageView.image = change[NSKeyValueChangeNewKey];
} else if ([keyPath isEqualToString:kTitleKeyPath]) {
tabBarItemView.titleLabel.text = change[NSKeyValueChangeNewKey];
} else if ([keyPath isEqualToString:kAccessibilityLabelKeyPath]) {
tabBarItemView.accessibilityLabel = change[NSKeyValueChangeNewKey];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,10 @@ - (CGSize)sizeThatFits:(CGSize)size {
return CGSizeMake(width, height);
}

#pragma mark - UIAccessibility

- (NSString *)accessibilityLabel {
return [super accessibilityLabel] ?: self.titleLabel.text;
}

@end

0 comments on commit 1d36b07

Please sign in to comment.