Skip to main content

How to mark content as a placeholder using redacted()

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to mark content as a placeholder using redacted() 관련

SwiftUI by Example

Back to Home

How to mark content as a placeholder using redacted() | SwiftUI by Example

How to mark content as a placeholder using redacted()

Updated for Xcode 15

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 projectopen in new window

A long gray rectangle representing redacted text.
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 projectopen in new window

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 projectopen in new window

The unredacted text “The rain in Spain falls mainly on the Spaniards”.
The unredacted text “The rain in Spain falls mainly on the Spaniards”.
Placeholder text “Loading...” standing in for redacted text.
Placeholder text “Loading...” standing in for redacted text.

Tips

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

Similar solutions…
How to mark content as private using privacySensitive() | SwiftUI by Example

How to mark content as private using privacySensitive()
How to add a placeholder to a TextField | SwiftUI by Example

How to add a placeholder to a TextField
How to create modifiers for a UIViewRepresentable struct | SwiftUI by Example

How to create modifiers for a UIViewRepresentable struct
How to render Markdown content in text | SwiftUI by Example

How to render Markdown content in text
How to create scrolling pages of content using tabViewStyle() | SwiftUI by Example

How to create scrolling pages of content using tabViewStyle()

이찬희 (MarkiiimarK)
Never Stop Learning.