3

The following code works great on iOS11 to detect if the user has set LARGE FONT in their accessibility settings.

However, I need to support this in iOS10 as well. How can I accomplish this?

Right now the code looks like this:

if #available(iOS 11.0, *) {
    if traitCollection.preferredContentSizeCategory.isAccessibilityCategory {
        return UITableViewAutomaticDimension
    } else {
        return someSpecificHeight
    }
} else {
    // how to detect is isAccessibilityCategory on non-iOS11 devices?
    // is there some ugly fallback I don't know about?
}
EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50
Jeff
  • 1,800
  • 8
  • 30
  • 54
  • If your labels don't have a height constraint and the constraints in your cell are correctly setup, your labels will expand to fit the size of the text. – EmilioPelaez Nov 19 '17 at 17:19

3 Answers3

2

Okay, based on @Jefflovejapan's answer, it looks like I can do this:

 let sizeCategory = traitCollection.preferredContentSizeCategory

        if sizeCategory == .accessibilityMedium
     || sizeCategory == .accessibilityLarge
     || sizeCategory == .accessibilityExtraLarge
     || sizeCategory == .accessibilityExtraExtraLarge
     || sizeCategory == .accessibilityExtraExtraExtraLarge {
            return UITableViewAutomaticDimension
        } else {
            return someSpecificHeight
        }

Ugly, but I think it does the trick..

I have to do all the == comparisons, because that seems like the only supported operator in iOS10 (all the other ones apparently are added in iOS11)

Jeff
  • 1,800
  • 8
  • 30
  • 54
  • 1
    The sizeCategory starting with .accessibilityMedium and above all appear to cause standard table cells with detailTextLabel on RHS of cell to add a line break and put the detailTextLabel below main label instead. UITableViewAutomaticDimension does correctly return taller cell heights when this happens, but the problem I was encountering was that it seems to return a minimum of 44px regardless of having smaller labels (without accessibility causing line break). Hence I was able to use above code to set "someSpecificHeight" to my normal 30px cell size, and then all worked as intended. – gpdawson Mar 25 '19 at 05:53
2

Expanding on Jeff's answer. I found it cleaner to add an extension to UITraitCollection. Hope this helps for any iOS dev still having to develop for iOS 10 (sad times).

extension UITraitCollection {
    var isAccessibleCategory: Bool {
        if #available(iOS 11, *) {
            return preferredContentSizeCategory.isAccessibilityCategory
        } else {
            switch preferredContentSizeCategory {
            case .accessibilityExtraExtraExtraLarge,
                 .accessibilityExtraExtraLarge,
                 .accessibilityExtraLarge,
                 .accessibilityLarge,
                 .accessibilityMedium:
                return true
            default:
                return false
            }
        }
    }
}

Can then be used like this

return traitCollection.isAccessibleCategory ? UITableView.automaticDimension : someSpecificHeight
Edward
  • 2,864
  • 2
  • 29
  • 39
1

It looks like .accessibilityMedium is the next size up from .extraExtraExtraLarge, so maybe that could be your threshold.

jefflovejapan
  • 2,047
  • 3
  • 20
  • 34