Created
November 2, 2021 05:53
-
-
Save sindresorhus/4e817cdac3b1f1ac7502b697e7b0d752 to your computer and use it in GitHub Desktop.
Nicer `Shape` access in SwiftUI
This file contains 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
/** | |
Before: | |
``` | |
.clipShape(Rectangle()) | |
``` | |
After: | |
``` | |
.clipShape(.rectangle) | |
``` | |
*/ | |
extension Shape where Self == Rectangle { | |
static var rectangle: Self { .init() } | |
} | |
extension Shape where Self == Circle { | |
static var circle: Self { .init() } | |
} | |
extension Shape where Self == Capsule { | |
static var capsule: Self { .init() } | |
} | |
extension Shape where Self == Ellipse { | |
static var ellipse: Self { .init() } | |
} | |
extension Shape where Self == ContainerRelativeShape { | |
static var containerRelative: Self { .init() } | |
} | |
extension Shape where Self == RoundedRectangle { | |
static func roundedRectangle(cornerRadius: Double, style: RoundedCornerStyle = .circular) -> Self { | |
.init(cornerRadius: cornerRadius, style: style) | |
} | |
static func roundedRectangle(cornerSize: CGSize, style: RoundedCornerStyle = .circular) -> Self { | |
.init(cornerSize: cornerSize, style: style) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment