美文网首页
Color Literal and Image Literal

Color Literal and Image Literal

作者: _浅墨_ | 来源:发表于2023-10-24 22:18 被阅读0次

Color Literal

Color Literal 允许我们直接从代码中使用颜色选择器选择颜色。我们可以选择颜色或键入十六进制代码,还可以设置不透明度。

color_literal

注意:Color Literal 非常适用于静态颜色。对于适应深色模式的颜色,建议在资源目录中使用颜色。

Color(#colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1))

Image Literal

类似于 Color Literal ,Image Literal 会打开一个图形用户界面,我们可以从其中选择资源目录中的所有图像。

image_literal

我们可以从 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)

相关文章

网友评论

      本文标题:Color Literal and Image Literal

      本文链接:https://www.haomeiwen.com/subject/wuasidtx.html