Nate
Nate

Reputation: 183

How do you set the accessibility label on an image that is drawn with Core Graphics?

I am trying to write UIAutomation tests around some core graphics logic. Currently we are using core graphics to draw an image. I'm trying to set the accessibility label/identifier/value on the image so that I can verify its presence via a UIAutomation test, but no matter what I do I'm not getting the accessibility label/identifier/value on the DOM in my test. Here are the things I have tried:

Setting directly on the image.

UIImage *uiImage = [UIImage imageWithData:bfCaseStudy.image];
uiImage.isAccessibilityElement = YES;
uiImage.accessibilityLabel = bfCaseStudy.name;
uiImage.accessibilityValue = bfCaseStudy.name;
uiImage.accessibilityIdentifier = bfCaseStudy.name;

CGContextDrawImage(context, [self rectForAttr:bfCaseStudy], uiImage.CGImage);

Setting on the Core Image

UIImage *uiImage = [UIImage imageWithData:bfCaseStudy.image];
uiImage.CIImage.isAccessibilityElement = YES;
uiImage.CIImage.accessibilityLabel = bfCaseStudy.name;
uiImage.CIImage.accessibilityValue = bfCaseStudy.name;

CGContextDrawImage(context, [self rectForAttr:bfCaseStudy], uiImage.CGImage);

Either way produces the same result. Here is the UIAutomation code trying to access the information.

UIALogger.logDebug(bookTwoHelper.mainWindow.images()[5].label());
UIALogger.logDebug(bookTwoHelper.mainWindow.images()[5].name());
UIALogger.logDebug(bookTwoHelper.mainWindow.images()[5].value());

Debug: (2013-02-25 16:06:33 +0000) - (null)
Debug: (2013-02-25 16:06:33 +0000) - (null)
Debug: (2013-02-25 16:06:33 +0000) - (null)

Here is the relevant portion of the DOM

UIAImage "(null)" {{0, 149}, {316, 55}}

Is there a way to set an accessibility label/identifier/value on an image that is drawn using core graphics?

Upvotes: 4

Views: 6392

Answers (1)

Mike Weller
Mike Weller

Reputation: 45598

Properties like accessibilityLabel will only work when used with UIKit controls like UIView, UIImageView etc. Because your image isn't a real UIView, you will need to provide extra information to UIKit by making your container view or view controller implement the UIAccessibilityContainer protocol.

See the Accessibility Programming Guide for iOS for more information, particularly Make the Contents of Custom Container Views Accessible.

Upvotes: 6

Related Questions