🔸template image
SwiftUI ⟩ control ⟩ Image ⟩ template image

SF Symbols:天生就是 template image。
// 1. 紅色的愛心
// foregroundColor() 設定不影響圖片顏色,愛心仍然是原來的顏色
image(color: .blue)
// 2. ⭐️ 設為 template image (愛心變藍色)
image(color: .blue, template: true)
// 3. ⭐️ SF Symbols 天生就是 template image
Image(systemName: "heart.fill")
// .font(.system(size: 150)) // frame 會超過 150
.resizable()
.scaledToFit()
.frame(height: 150)
.foregroundColor(.green)
.border(.yellow)
// private method
func image(color: Color, template: Bool = false) -> some View {
Image("heart")
// ⭐️ 設為 template image ?
.renderingMode(template ? .template : .original)
.resizable()
.scaledToFit()
.frame(width: 150, height: 150)
.foregroundColor(color) // 這個設定會讓愛心變成藍色
.border(.yellow)
}Gemini ⟩ 什麼是 template image?
Last updated
Was this helpful?