2 Star 23 Fork 10

小弟调调/swiftui-example

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
.github
.idoc
assets
example
accessibility
advanced-state
alerts-and-menus
animation
appendix-a
composing-views
containers
cross-platform
data
drawing
forms
images-shapes-media
introduction
lists
presenting-views
quick-start
responding-to-events
stacks-grids-scrollviews
taps-and-gestures
tooling
transforming-views
user-interface-controls
view-layout
working-with-static-text
demo1
demo10
demo11
demo12
demo2
demo3
demo4
demo5
demo6
demo7
demo8
demo9
Demo
README.md
cheat-sheet.md
.gitattributes
.gitignore
LICENSE
README.md
idoc.chapters.yml
idoc.yml
package.json
renovate.json
克隆/下载
README.md 1.72 KB
一键复制 编辑 原始数据 按行查看 历史

如何使用 redacted() 将内容标记为占位符?

Updated for Xcode 13 New in macOS 12

SwiftUI 允许我们在视图中将文本标记为占位符,这意味着它会被渲染,但会被灰色掩盖以表明它不是最终内容。 这是通过 redacted(reason:) 修饰符以及 unredacted() 修饰符提供的,您可以根据需要使用它们覆盖重写。

它在代码中的外观如下:

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

您只需在容器上使用 redacted(reason:),即可一次在视图中进行多次编辑,如下所示:

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

苹果公司表示,编辑是一个附加过程,这意味着,如果您向父母和孩子都添加编辑原因,那么它们将结合在一起。 目前只有 .placeholder,但是将来我们会看到像素化或类似现象吗?

您还可以查询从环境中传入的任何编校原因,如下所示:

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)
        }
    }
}

提示:使用与上面所示相同的代码,Redaction 也可用于图像。

Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Swift
1
https://gitee.com/jaywcjlove/swiftui-example.git
git@gitee.com:jaywcjlove/swiftui-example.git
jaywcjlove
swiftui-example
swiftui-example
main

搜索帮助