NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

How to mark content as a placeholder using redacted()

Paul Hudson    @twostraws   

Updated for Xcode 12.0

New in iOS 14

SwiftUI lets us mark text as a placeholder in our view, meaning that it gets rendered but masked out with gray to show it isn’t final content. This is provided through the redacted(reason:) modifier, which replaces the isPlaceholder() modifier they demonstrated at WWDC20.

Here’s how it looks in code:

Text("This is placeholder text")
    .redacted(reason: .placeholder)

You can redact several things in your view at once, just by using redacted(reason:) on a container, like this:

VStack {
    Text("This is placeholder text")
    Text("And so is this")
}
.redacted(reason: .placeholder)

Apple has said that redaction is an additive process, meaning that if you add redaction reasons to both a parent and a child then they will combine. Right now there’s only .placeholder, but perhaps we’ll see pixellation or similar in the future?

You can also query any redaction reasons passed in from the environment like this:

@Environment(\.redactionReasons) var redactionReasons

At this time you can’t redact images, which is a shame – hopefully this will appear in a future beta.

Subscribe to Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Link copied to your pasteboard.