Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Original article here: https://www.fivestars.blog/code/redacted-custom-effects.html
import SwiftUI
extension RedactionReasons {
public static let confidential = RedactionReasons(rawValue: 1 << 10)
}
struct ContentView: View {
var body: some View {
VStack {
Text("Hello world")
Text("Hello world")
.redactable()
.redacted(reason: .confidential)
}
.font(.title)
}
}
extension View {
func redactable() -> some View {
self
.modifier(Redactable())
}
}
struct Redactable: ViewModifier {
@Environment(\.redactionReasons) private var reasons
@ViewBuilder
func body(content: Content) -> some View {
if reasons.contains(.confidential) {
content
.accessibility(label: Text("Confidential"))
.overlay(Color.black)
} else {
content
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.padding()
.previewLayout(.sizeThatFits)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.