IconCard ๐ฆ
ไปฅใSF Symbol ็บๅๆฏ๏ผ้ๆๆน็ด็บ่ๆฏใ็ๅ ไปถ๏ผ้ฉๅ็จๆผ็ถ้ฎ็ฝฉๆๅ้ข้ฎ็ฝฉใ
Last updated
ไปฅใSF Symbol ็บๅๆฏ๏ผ้ๆๆน็ด็บ่ๆฏใ็ๅ ไปถ๏ผ้ฉๅ็จๆผ็ถ้ฎ็ฝฉๆๅ้ข้ฎ็ฝฉใ
Last updated
ๅฆๆ้ฎ็ฝฉๅ่่ฆๅช็็ดไธๆจฃๅคง๏ผ็จ SystemImage ๐ฆ ๅฐฑๅฏไปฅใ ๅฆๆ้ฎ็ฝฉๅ่ผๅฐใ่ฆๅช็็ด่ผๅคง๏ผ็จ IconCard ๐ฆ ใ
ไธๅ้กฏ็คบ img
(IconCard) ๅพๅฅ็จ .foregroundColor(.black)ใ.background(Color.white)ใ.compositingGroup()ใ.luminanceToAlpha()๏ผไธ็ดๅฐ paper
(็ด
่ฒๆ้ป็ด) ๅฅ็จ .mask() ็ๆดๅ้็จใ
้ๅ้็จๅพไพ่ขซๆฟ็ธฎๆไธๅ View extension๏ผ.inverseMask() ๐
import SwiftUI
import Workaround // for `SystemImage`
/// # ๐ฆ IconCard
/// - icon: SF Symbol ๅ็จฑใ
/// - size: โ ๏ธ ๆณจๆ๏ผ้ๆฏๅ็คบๅคงๅฐ๏ผไธๆฏๆดๅผตๅก็ๅคงๅฐโผ๏ธ
/// ## Example
/// ````
/// IconCard(icon: "heart.fill", size: 32)
/// ````
public struct IconCard: View {
// properties
let iconSize: CGFloat // icon size
let icon : String // icon name
/// public init
public init(_ icon: String, size: CGFloat = 32) {
self.icon = icon
self.iconSize = size
}
// body
public var body: some View {
// 1. ้ธ็จ ZStack๏ผ่ฎ็ฉไปถๅฏไปฅๅจไธไธๅฑค้่ผ้ฌๅ ็ใ
// โญ๏ธ ๅ
ไธ้ๅถ ZStack ็ๅคงๅฐ๏ผๅฎ็ๅคงๅฐ็ฑๅค้จ็ฐๅขๆฑบๅฎใ
ZStack {
// 2. ๅ
ๆบๅไธๅผตใ้ๆ็ดใ๏ผ้่ฃกๆๅ
ฉๅ็จๆ โญ๏ธ
// 1. ็จ Rectangle ไพๅฎ็พฉๆญคใๅ
ไปถ็ๅคงๅฐใ(ๅฆๅๆไปฅไธ้ข็ SystemImage ็บๆบ)ใ
// 2. ๅ
ๆๅฎ fg = .clear๏ผๅฐไพๅฆๆๆดๅๅ
ไปถๅ่ขซ่จญๅฎ .foreground() ๆ๏ผ
// - Rectangle ็ fg ้ๆฏ .clear (ไธๆ่ขซ overriddenโผ๏ธ)
// - SystemImage ็ fg ๅ ็บ้ๆฒ่ขซ่จญๅฎ้๏ผๆไปฅๅฐฑๅฏไปฅ่ขซๆนๅฏซใ
Rectangle()
.foregroundColor(.clear) // โญ๏ธ ๆๅฎใ้ๆ่ฒใ๏ผ
// 3. ็ถๅพๅจไธ้ข็ซๅ็คบ๏ผไธฆ่จญๅฎๅ็คบๅคงๅฐใ
// โญ๏ธ ๆณจๆ๏ผ้็ถๆฒๆๆๅฎๅๆฏ่ฒ๏ผไฝๅจๆทฑ่ฒๆจกๅผไธ๏ผๆ็จใ็ฝ่ฒใ่กจ็คบใ
SystemImage(icon)
.frame(width: iconSize, height: iconSize)
} // container (ZStack)
} // body
}
import SwiftUI
import Workaround // for `SystemImage`
/// # ๐ฆ IconCard
/// - icon: SF Symbol ๅ็จฑใ
/// - size: โ ๏ธ ๆณจๆ๏ผ้ๆฏๅ็คบๅคงๅฐ๏ผไธๆฏๆดๅผตๅก็ๅคงๅฐโผ๏ธ
/// ## Example
/// ````
/// IconCard(icon: "heart.fill", size: 32)
/// ````
public struct IconCard: View {
// properties
let iconSize: CGFloat // icon size
let icon : String // icon name
/// public init
public init(_ icon: String, size: CGFloat = 32) {
self.icon = icon
self.iconSize = size
}
// body
public var body: some View {
// 1. ้ธ็จ ZStack๏ผ่ฎ็ฉไปถๅฏไปฅๅจไธไธๅฑค้่ผ้ฌๅ ็ใ
// โญ๏ธ ๅ
ไธ้ๅถ ZStack ็ๅคงๅฐ๏ผๅฎ็ๅคงๅฐ็ฑๅค้จ็ฐๅขๆฑบๅฎใ
ZStack {
// 2. ๅ
ๆบๅไธๅผตใ้ๆ็ดใ๏ผ้่ฃกๆๅ
ฉๅ็จๆ โญ๏ธ
// 1. ็จ Rectangle ไพๅฎ็พฉๆญคใๅ
ไปถ็ๅคงๅฐใ(ๅฆๅๆไปฅไธ้ข็ SystemImage ็บๆบ)ใ
// 2. ๅ
ๆๅฎ fg = .clear๏ผๅฐไพๅฆๆๆดๅๅ
ไปถๅ่ขซ่จญๅฎ .foreground() ๆ๏ผ
// - Rectangle ็ fg ้ๆฏ .clear (ไธๆ่ขซ overriddenโผ๏ธ)
// - SystemImage ็ fg ๅ ็บ้ๆฒ่ขซ่จญๅฎ้๏ผๆไปฅๅฐฑๅฏไปฅ่ขซๆนๅฏซใ
Rectangle()
.foregroundColor(.clear) // โญ๏ธ ๆๅฎใ้ๆ่ฒใ๏ผ
// 3. ็ถๅพๅจไธ้ข็ซๅ็คบ๏ผไธฆ่จญๅฎๅ็คบๅคงๅฐใ
// โญ๏ธ ๆณจๆ๏ผ้็ถๆฒๆๆๅฎๅๆฏ่ฒ๏ผไฝๅจๆทฑ่ฒๆจกๅผไธ๏ผๆ็จใ็ฝ่ฒใ่กจ็คบใ
SystemImage(icon)
.frame(width: iconSize, height: iconSize)
} // container (ZStack)
} // body
}
extension View {
@ViewBuilder
func label(_ text: String) -> some View {
VStack(alignment: .leading) {
self.border(Color.black)
Text(text).padding(.leading).padding(.bottom, 10)
}.border(Color.black)
}
}
import SwiftUI
import PlaygroundSupport
let shad = Color.black.opacity(0.8)
let clear = Image("transparent")
let paper = Image("red-stars").resizable().scaledToFit()
.frame(maxWidth: 280, maxHeight: 200)
struct ContentView: View {
// 0. ่จญๅฎๅๅใๅ็คบๅคงๅฐใๆดๅๅ
ไปถๅคงๅฐใ
let mask: some View = IconCard("heart.fill", size: 100)
.frame(width: 150, height: 150)
// 1. ๅฅ็จ .foregroundColor()๏ผๅๆฏๅก้ปใ
// ๅฅ็จ .luminanceToAlph() ๅพ๏ผalpha = 0๏ผ่ฎใ้ๆใ๏ผ็ญๆผใๅๆฏ่ขซๆๆใใ
// โญ๏ธ ๆณจๆ๏ผๆญคๆไปคๆๅฅ็จๅฐใๆๆๅๅฑคใ๏ผไฝไธๆ override ๅทฒ็ถๆ fgColor ็ๅๅฑคใ
var v1: some View { mask.foregroundColor(.black) }
// 2. ่ๆฏๅก็ฝ
// ๅฅ็จ .luminanceToAlph() ๅพ๏ผalpha = 1๏ผๆไปฅใ่ๆฏๆไฟ็ไธไพใใ
var v2: some View { v1.background(Color.white) } // opacity = 1 (show)
// 3. ๅไฝตๆๆๅๅฑค
var v3: some View { v2.compositingGroup() }
// 4. ๅๅฐไบฎๅบฆ่ชฟ็บ alpha
var v4: some View { v3.luminanceToAlpha() }
var v5: some View {
ZStack {
// ็ด
่ฒๆ้ป็ด
paper
.mask(v4) // โญ๏ธ == paper.inverseMask(mask)
.shadow(color: shad, radius: 4, x: 4, y: 4)
// โญ๏ธ ็ซๅๅๅคๆก๏ผไฝไธ้กฏ็คบๅๅใ
mask
.hidden()
.border(Color.black)
} // ZStack
.background(Gradient.down(.yellow, .green))
}
var body: some View {
VStack(alignment: .leading) {
HStack {
mask.label("img")
v1 .label("fg = black")
v2 .label("bg = white")
}
HStack(alignment: .top) {
v4
.background(clear) // โญ๏ธ ๅบไธ้ชใ้ๆๆๆ ผ็ดใ
.clipped() // โญ๏ธ ่ฃๆ่ถ
ๅบ v4 ็้จๅ
.label("ไบฎๅบฆ่ฝ alpha")
v5.label("paper.inverseMask(img)")
}
} // container (HStack)
.padding()
.background(Color.gray)
} // body
}
PlaygroundPage.current.setLiveView(ContentView())