A Canvas
receives a GraphicsContext
as closure parameter and this can be used to draw and fill shapes.
If a path is filled with a Material
style, it just fills with solid black, instead of the usual translucent effect:
struct ContentView: View {
let loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
var body: some View {
Text(loremIpsum)
.font(.title2)
.overlay {
Canvas { ctx, size in
ctx.fill(
Path(
roundedRect: CGRect(
x: (size.width - 200) / 2,
y: (size.height - 100) / 2 ,
width: 200,
height: 100
),
cornerRadius: 10
),
with: .style(.regularMaterial)
)
}
}
.padding()
}
}
I tried using a .palette
too, but then the Material
effect is ignored (there is no blurring):
with: .palette([.color(white: 0, opacity: 0.2), .style(.regularMaterial)])
How can a GraphicsContext
be used to fill a Path
with a Material
style to give the usual translucent effect?