Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@zntfdr
Last active July 30, 2020 00:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zntfdr/c56ed8f9190f8416baa7527359e78ed6 to your computer and use it in GitHub Desktop.
Save zntfdr/c56ed8f9190f8416baa7527359e78ed6 to your computer and use it in GitHub Desktop.
// 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