Created
May 7, 2021 17:09
-
-
Save davidsteppenbeck/3c9a731688e5774946db9d5221174ba9 to your computer and use it in GitHub Desktop.
A SwiftUI view modifier to limit the content size category of a view to a specified maximum size.
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 SwiftUI | |
fileprivate struct ContentSizeCategoryModifier: ViewModifier { | |
// MARK:- Properties | |
/// The preferred size of the content. | |
@Environment(\.sizeCategory) private var sizeCategory | |
/// The maximum permitted content size category. | |
let sizeCategoryLimit: ContentSizeCategory | |
// MARK:- Methods | |
func body(content: Content) -> some View { | |
content.environment(\.sizeCategory, sizeCategory > sizeCategoryLimit ? sizeCategoryLimit : sizeCategory) | |
} | |
} | |
extension View { | |
/// Limits the content size category of the view to the specified value. | |
/// | |
/// - Parameters: | |
/// - sizeCategoryLimit: The maximum permitted content size category. | |
func contentSizeCategoryLimited(to sizeCategoryLimit: ContentSizeCategory) -> some View { | |
modifier(ContentSizeCategoryModifier(sizeCategoryLimit: sizeCategoryLimit)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment