Logo

dev-resources.site

for different kinds of informations.

Making Accessible Links in SwiftUI

Published at
12/18/2024
Categories
swiftui
ios
a11y
Author
hborawski
Categories
3 categories in total
swiftui
open
ios
open
a11y
open
Author
9 person written this
hborawski
open
Making Accessible Links in SwiftUI

SwiftUI has streamlined Apple platform UI development, but it's not without its flaws, especially regarding accessibility. A notable concern is the inaccessibility of links within the Text view—screen readers fail to recognize these inline links as actionable elements. In this post, we'll dissect this issue, exploring its impact on user experience and providing guidelines on how developers can enhance accessibility (a11y) in their SwiftUI applications.

Problem

Let's start with some simple code to illustrate the problem. Here is a simple link within text, rendered using Markdown.

let url = URL(string: "https://developer.apple.com/")!
Text("Please review our [Terms and Conditions](\(url.absoluteString))")
Enter fullscreen mode Exit fullscreen mode

This should look like this:

A screenshot from a iPhone simulator that shows a line of text saying 'Please review our Terms and Conditions'. 'Terms and Conditions' is styled as a link in a different color.

As you can imagine, inlining links with text is a common use case across apps. They're used for user agreements, deep-linking other pages, and simply linking to external articles.

However, when links are added like this, it is not clear to screen-readers that they are tappable. Using the Accessibility Inspector, we can see this issue in action for the code above:

The inspected element is 'Please review our Terms and Conditions, text'. It is of type 'text'.

"Please review our Terms and Conditions" is one label (good) with type 'text' (not good). The screen-reader will not announce this as interactive.

Solutions

Solution 1: Add a Button Trait

Adding a button trait to the Text element will ensure the screen-reader knows the element is interactive. Here’s how you could implement it:

let url = URL(string: "https://developer.apple.com/")!
Text("Please review our [Terms and Conditions](\(url.absoluteString))")
    .accessibilityAddTraits(.isButton)
Enter fullscreen mode Exit fullscreen mode

However, when a text segment contains multiple links, differentiating between them becomes a challenge. The entire text block is treated as a single UI element, which isn't ideal when you want users to choose between multiple links. This solution can make the text look like a button to screen-readers, but it doesn't provide the granularity needed for multiple links within the same text.

Solution 2: Redesign the User Interface

A more robust but labor-intensive solution is to redesign the user interface so that links do not have to be appended with additional text. By isolating links from other text, you can define each link as a SwiftUI Link, which is inherently more accessible. (A Link will be presented to screen-readers as a button.)

Link("Terms and Conditions", destination: URL(string: "https://developer.apple.com/")!)
Enter fullscreen mode Exit fullscreen mode

This approach maintains the visual and functional distinction of each link, improving accessibility by making each link individually selectable and actionable.

However, redesigning the user interface may not always be feasible. It can involve significant changes to the app's design and layout, which may affect the project timeline, resources, or even the overall user experience.

(For products that expect public usage, ensuring accessibility should be a priority from the outset of the design process, as retrofitting solutions often leads to compromises in user experience and functionality.)

Conclusion

Both solutions offer ways to make inline links more accessible in SwiftUI, though each comes with its own set of trade-offs. The choice between adding a button trait to the entire text block and redesigning the UI to isolate links should be made based on the specific needs of the project, the frequency and nature of the links in question, and the available resources. Of course, there are probably many other solutions as well, and I hope this article helps you on the path to exploring them!


About the Author

Koriann (like the "corian" in "coriander") South is an accomplished iOS developer with a passion for crafting seamless and accessible user interfaces. Since joining Intuit in 2022, she has played a pivotal role in the development of the company's design system, ensuring that it meets the highest standards of accessibility and usability. With a keen eye for detail and a commitment to inclusivity, Koriann's work helps ensure that Intuit's products are accessible to millions of users worldwide. In her spare time, Koriann enjoys exploring the latest advancements in technology and sharing her knowledge with the developer community.


AI Acknowledgement

This was written with the help of ChatGPT. I wrote the draft, then I used ChatGPT to rephrase my sentences, and then I rewrote the rephrasing I was unsatisified with and added a few more sentences for flavour.

a11y Article's
30 articles in total
Favicon
Why are we so rubbish at accessibility?
Favicon
Web Accessibility: Who's it for?
Favicon
I Created VanillaHTML (a CSS File)
Favicon
Leveraging AWS Services for Accessible Cloud Solutions
Favicon
Preparing your business for the European Accessibility Act
Favicon
How to Automatically Generate Alt Text for Images Using Amazon Rekognition
Favicon
Support Time to Take Action with Compose
Favicon
Web Development in Healthcare: Compliance and Best Practices for Healthcare Websites
Favicon
Mobile Accessibility Advent Calendar Part 2
Favicon
Inclusive Design: How to Build Web Applications That Welcome Everyone
Favicon
Accessible Color Contrast: Why It Matters and How to Get It Right
Favicon
a11y. Cómo solucionar los 6 errores más comunes de accesibilidad web
Favicon
Making Accessible Links in SwiftUI
Favicon
Native HTML: Accordion Revisited
Favicon
5 accessibility defects LinkedIn could resolve to make content more accessible
Favicon
User-Centered Design: The Secret to Intuitive, User-Friendly Interfaces
Favicon
Mobile Accessibility Advent Calendar Part 1
Favicon
Top 10 Web Accessibility Mistakes Developers Make and How to Avoid Them
Favicon
🚀 React Best Practices for Scalable Frontends: Part 3 – Lazy Loading and Accessibility
Favicon
WICK-A11Y 1.4.0: Not Everything Needs to Fail a Test!
Favicon
Introduction to ARIA: A Developer’s Guide
Favicon
Balancing Visual Design and Optimization in Front-End Development
Favicon
How to Test Web Accessibility Using the WAVE Tool: A Beginner's Guide
Favicon
Building Accessible Forms: Best Practices for Usability
Favicon
Accessibility on web
Favicon
Automated Testing with jest-axe
Favicon
Accessible Input Elements | the Basics
Favicon
Accessible, sustainable, and creative web development
Favicon
Accessibility (a11y) Rules - 3
Favicon
Accessibility (a11y) Rules - 4

Featured ones: