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
demo1
demo10
demo11
demo12
demo13
demo14
demo15
demo16
demo17
demo18
demo19
demo2
Demo
README.md
demo20
demo21
demo22
demo3
demo4
demo5
demo6
demo7
demo8
demo9
user-interface-controls
view-layout
working-with-static-text
cheat-sheet.md
.gitattributes
.gitignore
LICENSE
README.md
idoc.chapters.yml
idoc.yml
package.json
renovate.json
克隆/下载
README.md 1.11 KB
一键复制 编辑 原始数据 按行查看 历史

如何为视图周围的填充着色?

padding() 修改器使我们可以在视图周围添加一些间距,而 background() 修改器使我们可以设置背景颜色。 但是,使用它们的方式很重要,因此务必明确目标,以便获得最佳结果。

例如,这将创建一个具有黑色背景和白色前景的文本视图,然后向其添加系统默认填充:

Text("Examples with Swift")
    .background(Color.red)
    .foregroundColor(.white)
    .padding()

这会添加系统默认填充,然后设置黑色背景颜色和白色前景:

Text("Examples with Swift")
    .padding()
    .background(Color.red)
    .foregroundColor(.white)

这两段代码可能看起来相似,但是它们产生不同的结果,因为应用修饰符的顺序很重要。 在第二个示例中,视图被填充然后被着色,这意味着填充也被着色为黑色。 相比之下,第一个示例将填充颜色,因此填充保持不变色。

因此,如果您希望某些文本的背景颜色比文本本身宽,请确保使用第二个代码示例-先填充然后上色。

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

搜索帮助