Skip to content

Instantly share code, notes, and snippets.

@alloy
Created May 3, 2011 13:28
Show Gist options
  • Save alloy/953326 to your computer and use it in GitHub Desktop.
Save alloy/953326 to your computer and use it in GitHub Desktop.
A UIActionSheet subclass that customizes the accessibility labels of the options in the sheet.
// Copyright (c) 2011 Eloy Durán <[email protected]>
// Available under the MIT license: http://www.opensource.org/licenses/mit-license.php
#import <objc/message.h>
@interface FTAccessibleActionSheet : UIActionSheet
- (void)copyAccessibilityMetadataFrom:(NSString *)title toControl:(UIView *)control;
@end
@implementation FTAccessibleActionSheet
// When the action sheet is shown with the device in `landscape' mode, it uses
// a table view to show the options. So we override this delegate method which
// gives us the chance to provide the correct accessibility labels.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// Just to be safe if Apple decides to no longer implement this delegate
// method in UIActionSheet.
SEL sel = @selector(tableView:willDisplayCell:forRowAtIndexPath:);
if ([super respondsToSelector:sel]) {
struct objc_super target = { self, [self superclass] };
objc_msgSendSuper(&target, sel, tableView, cell, indexPath);
}
// The label seems to always be the last subview, but again let's ensure that
// this implementation detail does not matter to us.
for (UIView *view in cell.subviews) {
if ([view isKindOfClass:[UILabel class]]) {
[self copyAccessibilityMetadataFrom:[(UILabel *)view text] toControl:view];
return;
}
}
}
- (NSInteger)addButtonWithTitle:(NSString *)title {
NSInteger i = [super addButtonWithTitle:title];
// After letting the superclass add the button, search for it by title.
for (UIView *view in self.subviews) {
if ([view respondsToSelector:@selector(title)]) {
NSString *controlTitle = [view performSelector:@selector(title)];
if ([title isEqualToString:controlTitle]) {
[self copyAccessibilityMetadataFrom:title toControl:view];
return i;
}
}
}
return i;
}
// Copy the actual accessibility data from the title string, which is what UISegmentedControl does.
//
// For example:
//
// NSString *title = @"2:3";
// title.accessibilityLabel = @"two by one";
// [actionSheet addButtonWithTitle:title];
- (void)copyAccessibilityMetadataFrom:(NSString *)title toControl:(UIView *)control {
control.accessibilityLanguage = title.accessibilityLanguage;
control.accessibilityLabel = title.accessibilityLabel;
}
@end
@alloy
Copy link
Author

alloy commented May 3, 2011

Filed a radar for this issue: http://openradar.appspot.com/radar?id=1174411

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment