Skip to content

Commit 5dd23b8

Browse files
author
Dave Alden
committed
Support custom accessibility descriptions for buttons on iOS
1 parent 09ce277 commit 5dd23b8

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/ios/CDVThemeableBrowser.m

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Licensed to the Apache Software Foundation (ASF) under one
4646
#define kThemeableBrowserPropTitle @"title"
4747
#define kThemeableBrowserPropCancel @"cancel"
4848
#define kThemeableBrowserPropItems @"items"
49+
#define kThemeableBrowserPropAccessibilityDescription @"accessibilityDescription"
4950

5051
#define kThemeableBrowserEmitError @"ThemeableBrowserError"
5152
#define kThemeableBrowserEmitWarning @"ThemeableBrowserWarning"
@@ -514,7 +515,7 @@ - (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*
514515
[self.commandDelegate sendPluginResult:pluginResult callbackId:scriptCallbackId];
515516
return NO;
516517
}else if ([scriptCallbackId isEqualToString:@"message"] && (self.callbackId != nil)) {
517-
// Send a message event
518+
// Send a message event
518519
NSString* scriptResult = [url path];
519520
if ((scriptResult != nil) && ([scriptResult length] > 1)) {
520521
scriptResult = [scriptResult substringFromIndex:1];
@@ -525,7 +526,7 @@ - (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*
525526
[dResult setValue:@"message" forKey:@"type"];
526527
[dResult setObject:decodedResult forKey:@"data"];
527528
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dResult];
528-
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
529+
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
529530
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
530531
}
531532
}
@@ -756,7 +757,8 @@ - (void)createViews
756757
if (toolbarProps[kThemeableBrowserPropImage] || toolbarProps[kThemeableBrowserPropWwwImage]) {
757758
UIImage *image = [self getImage:toolbarProps[kThemeableBrowserPropImage]
758759
altPath:toolbarProps[kThemeableBrowserPropWwwImage]
759-
altDensity:[toolbarProps[kThemeableBrowserPropWwwImageDensity] doubleValue]];
760+
altDensity:[toolbarProps[kThemeableBrowserPropWwwImageDensity] doubleValue]
761+
accessibilityDescription:@""];
760762

761763
if (image) {
762764
self.toolbar.backgroundColor = [UIColor colorWithPatternImage:image];
@@ -934,7 +936,7 @@ - (void)createViews
934936
* bundle, we can't tell what densitiy the image is supposed to be so it needs to be given
935937
* explicitly.
936938
*/
937-
- (UIImage*) getImage:(NSString*) name altPath:(NSString*) altPath altDensity:(CGFloat) altDensity
939+
- (UIImage*) getImage:(NSString*) name altPath:(NSString*) altPath altDensity:(CGFloat) altDensity accessibilityDescription:(NSString*) accessibilityDescription
938940
{
939941
UIImage* result = nil;
940942
if (name) {
@@ -947,6 +949,8 @@ - (UIImage*) getImage:(NSString*) name altPath:(NSString*) altPath altDensity:(C
947949
}
948950
NSData* data = [NSData dataWithContentsOfFile:path];
949951
result = [UIImage imageWithData:data scale:altDensity];
952+
result.accessibilityLabel = accessibilityDescription;
953+
result.isAccessibilityElement = true;
950954
}
951955

952956
return result;
@@ -957,10 +961,16 @@ - (UIButton*) createButton:(NSDictionary*) buttonProps action:(SEL)action withDe
957961
UIButton* result = nil;
958962
if (buttonProps) {
959963
UIImage *buttonImage = nil;
964+
NSString* accessibilityDescription = description;
965+
if(buttonProps[kThemeableBrowserPropAccessibilityDescription]){
966+
accessibilityDescription = buttonProps[kThemeableBrowserPropAccessibilityDescription];
967+
}
960968
if (buttonProps[kThemeableBrowserPropImage] || buttonProps[kThemeableBrowserPropWwwImage]) {
961969
buttonImage = [self getImage:buttonProps[kThemeableBrowserPropImage]
962970
altPath:buttonProps[kThemeableBrowserPropWwwImage]
963-
altDensity:[buttonProps[kThemeableBrowserPropWwwImageDensity] doubleValue]];
971+
altDensity:[buttonProps[kThemeableBrowserPropWwwImageDensity] doubleValue]
972+
accessibilityDescription: accessibilityDescription
973+
];
964974

965975
if (!buttonImage) {
966976
[self.navigationDelegate emitError:kThemeableBrowserEmitCodeLoadFail
@@ -978,7 +988,9 @@ - (UIButton*) createButton:(NSDictionary*) buttonProps action:(SEL)action withDe
978988
if (buttonProps[kThemeableBrowserPropImagePressed] || buttonProps[kThemeableBrowserPropWwwImagePressed]) {
979989
buttonImagePressed = [self getImage:buttonProps[kThemeableBrowserPropImagePressed]
980990
altPath:buttonProps[kThemeableBrowserPropWwwImagePressed]
981-
altDensity:[buttonProps[kThemeableBrowserPropWwwImageDensity] doubleValue]];;
991+
altDensity:[buttonProps[kThemeableBrowserPropWwwImageDensity] doubleValue]
992+
accessibilityDescription: accessibilityDescription
993+
];;
982994

983995
if (!buttonImagePressed) {
984996
[self.navigationDelegate emitError:kThemeableBrowserEmitCodeLoadFail
@@ -1699,3 +1711,4 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
16991711

17001712

17011713
@end
1714+

0 commit comments

Comments
 (0)