Last active
July 30, 2020 00:18
-
-
Save zntfdr/c56ed8f9190f8416baa7527359e78ed6 to your computer and use it in GitHub Desktop.
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
// 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