Color Literal
Color Literal 允许我们直接从代码中使用颜色选择器选择颜色。我们可以选择颜色或键入十六进制代码,还可以设置不透明度。
![](https://img.haomeiwen.com/i130752/b976d94b94818381.jpg)
注意:Color Literal 非常适用于静态颜色。对于适应深色模式的颜色,建议在资源目录中使用颜色。
Color(#colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1))
Image Literal
类似于 Color Literal ,Image Literal 会打开一个图形用户界面,我们可以从其中选择资源目录中的所有图像。
![](https://img.haomeiwen.com/i130752/11e8685c4ccc9975.jpg)
我们可以从 Unsplash 网站下载所需图像,然后放入到项目中使用。
Image(uiImage: #imageLiteral(resourceName: "YOUR-IMAGE"))
最终示例
在最终布局中,我们可以快速更改标题的颜色和卡片的背景图像。
VStack {
Text("Color literal")
.font(.title).bold()
.foregroundColor(Color(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)))
}
.frame(width: 300, height: 200)
.background(Image(uiImage: #imageLiteral(resourceName: "gradienta-ix_kUDzCczo-unsplash")).resizable().aspectRatio(contentMode: .fill))
.cornerRadius(20)
网友评论