Created
February 7, 2019 02:51
-
-
Save mntone/fbb964ef1cce0c0f2bb9217aa35a72c6 to your computer and use it in GitHub Desktop.
UITraitCollection compatibility extension. under MIT license.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| extension UITraitCollection { | |
| public var compatible: UITraitCollectionCompatible { | |
| return UITraitCollectionCompatible(self) | |
| } | |
| } | |
| public struct UITraitCollectionCompatible { | |
| private let traitCollection: UITraitCollection | |
| fileprivate init(_ traitCollection: UITraitCollection) { | |
| self.traitCollection = traitCollection | |
| } | |
| @available(iOS 9.0, *) | |
| public var preferredContentSizeCategory: UIContentSizeCategoryCompatible { | |
| let contentSizeCategory: UIContentSizeCategory | |
| if #available(iOS 10.0, *) { | |
| contentSizeCategory = traitCollection.preferredContentSizeCategory | |
| } else { | |
| contentSizeCategory = UITraitCollectionCompatible.getContentSizeCategory() | |
| } | |
| return UIContentSizeCategoryCompatible(contentSizeCategory) | |
| } | |
| private static func getContentSizeCategory() -> UIContentSizeCategory { | |
| let fontSize = UIFont.preferredFont(forTextStyle: .body).pointSize | |
| switch fontSize { | |
| case 14.0: return .extraSmall | |
| case 15.0: return .small | |
| case 16.0: return .medium | |
| case 17.0: return .large | |
| case 19.0: return .extraLarge | |
| case 21.0: return .extraExtraLarge | |
| case 23.0: return .extraExtraExtraLarge | |
| case 28.0: return .accessibilityMedium | |
| case 33.0: return .accessibilityLarge | |
| case 40.0: return .accessibilityExtraLarge | |
| case 47.0: return .accessibilityExtraExtraLarge | |
| case 53.0: return .accessibilityExtraExtraExtraLarge | |
| default: return .medium | |
| } | |
| } | |
| } | |
| @available(iOS 9.0, *) | |
| public struct UIContentSizeCategoryCompatible { | |
| public let rawValue: UIContentSizeCategory | |
| fileprivate init(_ contentSizeCategory: UIContentSizeCategory) { | |
| self.rawValue = contentSizeCategory | |
| } | |
| public var isAccessibilityCategory: Bool { | |
| if #available(iOS 11.0, *) { | |
| return rawValue.isAccessibilityCategory | |
| } else { | |
| switch rawValue { | |
| case .accessibilityMedium: fallthrough | |
| case .accessibilityLarge: fallthrough | |
| case .accessibilityExtraLarge: fallthrough | |
| case .accessibilityExtraExtraLarge: fallthrough | |
| case .accessibilityExtraExtraExtraLarge: | |
| return true | |
| default: | |
| return false | |
| } | |
| } | |
| } | |
| } | |
| // MARK: - Equatable | |
| extension UIContentSizeCategoryCompatible: Equatable { | |
| public static func == (lhs: UIContentSizeCategoryCompatible, rhs: UIContentSizeCategoryCompatible) -> Bool { | |
| return lhs.rawValue == rhs.rawValue | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment