👔Swatch

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>
Last updated
Was this helpful?