iOS Attributed Accessibility Labels

Rob Whitaker
Mac O’Clock
Published in
3 min readMay 4, 2020

--

Photo by Zan on Unsplash

Attributed accessibility labels are an incredible tool for making some next-level accessible experiences. They let you tell VoiceOver not just what to speak, but how to say it too.

Using the accessibilityAttributedLabel property you can provide an NSAttributedString to VoiceOver, much the same way you would provide an NSAttributedString to a label’s attributedText property to display a string with an underline or character colour for example. The difference here is that all of our attributes are instructions for VoiceOver.

In the below example, we’re adding a pitch change to the word ‘best’ where in displayed text we might add a bold attribute to add emphasis. This means that your VoiceOver users will get a comparable experience to sighted users rather than a cut down one. This is the ultimate aim of digital accessibility. Create a comparable, inclusive interaction with your users, regardless of their abilities. Always ensure your text is localised and perform proper range calculations in your code, rather than hard coding everything as I am in this example.

let attributedString = NSMutableAttributedString(string: "This
is the best app on the App Store!")
let range = attributedString.string.range(of: "best")
attributedString.addAttributes([. accessibilitySpeechPitch: 1.5], range: NSRange(range!, in…

--

--

Rob Whitaker
Mac O’Clock

iOS Engineer at Capital One. Author, Developing Inclusive Mobile Apps, Apress. https://amzn.to/3aNRQ6d