BLACK FRIDAY: Save 50% on all my books and bundles! >>

How to mark content as a placeholder using redacted()

Paul Hudson    @twostraws   

Updated for Xcode 16.4

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, along with an unredacted() modifier you can use to override redaction as needed.

Here’s how it looks in code:

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

Download this as an Xcode project

A long gray rectangle representing redacted text.

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") } .font(.title) .redacted(reason: .placeholder)

Download this as an Xcode project

Two gray rectangles representing two lines of redacted text.

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:

struct ContentView: View { @Environment(\.redactionReasons) var redactionReasons let bio = "The rain in Spain falls mainly on the Spaniards" var body: some View { if redactionReasons == .placeholder { Text("Loading…") } else { Text(bio) .redacted(reason: redactionReasons) } } }

Download this as an Xcode project

The unredacted text “The rain in Spain falls mainly on the Spaniards”.

Placeholder text “Loading...” standing in for redacted text.

Tip: Redaction also works on images using the same code as shown above.

Save 50% in my Black Friday sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further for less! Get my all-new book Everything but the Code to make more money with apps, get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn Swift Testing, design patterns, and more.

Save 50% on all our books and bundles!

Similar solutions…

BUY OUR BOOKS
Buy Everything but the Code Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Interview Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift 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 Beyond Code

Was this page useful? Let us know!

Average rating: 4.6/5

 
Unknown user

You are not logged in

Log in or create account