๐Swatch
Last updated
Last updated
swiftโฉ custom โฉ view โฉ Swatch
ๅฏ้กฏ็คบไธๅฐๅกๅฑ็คบๅ่ๅ็จฑ็่ฒๅก๏ผไฝไธๆญขๆผ้กฏ็คบ้ก่ฒ๏ผไปปไฝ view ้ฝๅฏไปฅใ
import SwiftUI
struct Swatch<V: View>: View {
// font: Avenir Next Condensed
let avenir = Font.custom("AvenirNextCondensed-Regular", size: 14)
let size: CGFloat // swatch size
let name: String // view name
let shadow: Color // border inner shadow
let border: Color // border color
let view: () -> V // swatch view
init(
name : String,
size : CGFloat = 80,
shadow : Color = .bg2, // ๐ Color + system colors
border : Color = .bg3,
view: @escaping () -> V
){
self.size = size
self.name = name
self.shadow = shadow
self.border = border
self.view = view
}
var body: some View {
VStack(spacing: 0) {
// swatch view
view()
// โญ๏ธ overlay shadow + blend
.overlay {
Rectangle()
.fill(.white.shadow(.inner(
color: shadow, radius: 2, x: 2, y: -2
)))
// โญ๏ธ ๅฟ
้ ็จ .blendMode() ๅฆๅๅกซๅ
่ฒๆ่ไฝๅๆฏ
.blendMode(.multiply)
}
.frame(width: size, height: size)
.border(border)
// swatch name
// ไธ่ฆ็ดๆฅ็จ Text๏ผๅฆๅๆๅ ็บ Text ็้ทๅบฆไธไธ่ๅฝฑ้ฟ็้ขโ๏ธ
Rectangle()
.fill(.clear)
.frame(width: size, height: 20)
.overlay {
Text("\(name)")
.font(avenir)
.fixedSize() // โญ๏ธ no wrap
}
}
.padding(4)
.border(border)
}
}// Swatch<V>
// example
struct SwatchExample: View {
let colors = Color.standardColors.first(11) // ๐ Array + .first(_ size:)
var body: some View {
VStack(spacing: 16) {
// colors
HStack(spacing: 12) { ForEach(colors, id: \.self) { color in
Swatch(name: color.name, border: .label4) { color } // ๐ custom view
}}
// gradient of colors
HStack(spacing: 12) { ForEach(colors, id: \.self) { color in
Swatch(name: "\(color.name).gradient", border: .label4) { color.gradient }
}}
}// VStack
}// body
}
// previews
struct Swatch_Previews: PreviewProvider {
static var previews: some View {
SwatchExample()
}
}
arr.first()๏ผ array first n elements
system colors๏ผ ่ฒๅก้ก่ฒไพๆบ