extension View {
func insideBorder<C, S>(
_ content: C,
shape: S = Rectangle(),
lineWidth: CGFloat = 1
) -> some View where C: ShapeStyle, S: InsettableShape {
overlay(
shape
.strokeBorder(content, lineWidth: lineWidth)
)
}
func halfBorder<C, S>(
_ content: C,
shape: S = Rectangle(),
lineWidth: CGFloat = 1
) -> some View where C: ShapeStyle, S: InsettableShape {
overlay(
shape
.stroke(content, lineWidth: lineWidth)
)
}
func outsideBorder<C, S>(
_ content: C,
shape: S = Rectangle(),
lineWidth: CGFloat = 1
) -> some View where C: ShapeStyle, S: InsettableShape {
overlay(
shape
.stroke(content, lineWidth: lineWidth * 2)
.overlay(self)
)
}
}
// Example
HStack {
Spacer()
Rectangle()
.fill(.orange)
.frame(width: 60, height: 60)
Spacer()
Rectangle()
.fill(.orange)
.frame(width: 60, height: 60)
.insideBorder(Color.red.opacity(0.5), lineWidth: 10)
Spacer()
Rectangle()
.fill(.orange)
.frame(width: 60, height: 60)
.halfBorder(Color.red.opacity(0.5), lineWidth: 10)
Spacer()
Rectangle()
.fill(.orange)
.frame(width: 60, height: 60)
.outsideBorder(Color.red.opacity(0.5), lineWidth: 10)
Spacer()
}
'iOS > SwiftUI' 카테고리의 다른 글
SwiftUI 텍스트 포맷 빠르게 알아보기 (0) | 2024.06.14 |
---|---|
SwiftUI의 AttributedString사용해보기 (feat. hacking with swift) (0) | 2024.01.25 |
SwiftUI에서 GoogleMapsAPI 적용해보기 - 심화 (0) | 2023.02.22 |
SwiftUI에서 GoogleMapsAPI 적용해보기 - 기초 (0) | 2023.02.19 |
Alignment Guides (0) | 2022.07.11 |