Monday, January 13, 2025

Accessibility That Fits

Soroush Khanlou:

Building a design that’s responsive to both its contents and its environment is a one of the primary challenges of robust user interface programming. There are some false gods out there and some legitimate best practices. However, I’ve found a new strategy that really helps, especially for text that has been scaled up for accessibility reasons.

[…]

So it’s not going to work to just shorten the string a little bit to get the text to fit. It is an interesting idea, though, that you can make the date take up less space by configuring it slightly differently. How do you get it to show the narrower string only if it needs to? You could try to key it off the dynamicTypeSize, which you can pull out of the environment:

@Environment(\.dynamicTypeSize) var dynamicTypeSize

But, this would ultimately be vague guesswork, and might break based on other factors, like screen size.

Fortunately, SwiftUI comes with a tool that helps us pick the best fitting option out of a series of compatible views, and it’s called ViewThatFits.

I’ve usually seen ViewThatFits discussed as a way to make a responsive layout that changes shape based on the available space. But it also seems to be an elegant solution for more mundane situations of figuring out how much text can fit—in this case choosing which date format to use.

I was going to say that Apple has used auto-changing date formats for decades, e.g. in Mail’s table view, but upon checking it looks like Apple removed that when it removed the Mail table view in Catalina and didn’t restore it when bringing back the table view a few years later. The column no longer separately aligns the days and times, either.

Finder does still adjust date formats based on the table column width, and it was not uncommon for apps to implement this themselves when I was initially developing EagleFiler, but Apple never added it as a standard framework feature, and it seems less common these days.

1 Comment RSS · Twitter · Mastodon


I hate that they removed that in Mail. We still use DateCell.h/.m in our app from ye olde Apple sample code, which of course is long gone on apple.com but it's this (I didn't realize they had updated it since 2007):

https://github.com/brock8503/CocoaSampleCode/blob/master/PhotoSearch/DateCell.m

Leave a Comment