[Swift] μ¬μ©μ 컬λ¬μ μΆκ°νκ³ UI Color νμ₯νμ¬ μ½λλ‘ μ κ·Όνκ² λ§λ€κΈ°
μ± μ€νμΌμ λ°λΌ μ λ°μ μΌλ‘ λ°λ³΅λμ΄ μ¬μ©νλ 컬λ¬λ₯Ό Assetsμ μΆκ°νλ©΄, μΈν°νμ΄μ€ λΉλμ μμμ΄ μΆκ°λμ΄ μΈν°νμ΄μ€λ₯Ό ꡬμ±ν μμ κ°νΈνκ² μμ μΆκ°νκ³ κ΄λ¦¬ν μ μμ΅λλ€. κ²μκΈμμλ μμμ Assets μ μΆκ°νκ³ , μΈν°νμ΄μ€ λΉλλ‘ μ¬μ©ν΄λ³΄λ©°, UIColorλ₯Ό extension νμ₯νμ¬ μ½λ μμμΌλ‘λ μ κ·Όν μ μλλ‘ νλ λ°©λ²μ λ€λ£Ήλλ€.
Assets/ μ μΆκ°νκΈ°
Xcode μ’μΈ‘μ μλ Project Navigaterμμ Assets.xcassets μ λ€μ΄κ° ν ν΄λ Colors λ₯Ό λ§λ€μ΄μ€λλ€. ν΄λΉ ν΄λ μμ λ§μ°μ€ μ€λ₯Έμͺ½ λ²νΌμ λλ¬ [New Color Set]μ λλ¬μ€λλ€.
μνλ 컬λ¬μ μ΄λ¦μ μ§μ νλ©΄ μλμ κ°μ΄ 컬λ¬μΉ©μ΄ λΉλλ€. ν΄λΉ 컬λ¬μ μ Attribute Inspector μμμμ μΈν λ°©λ²μ μ ννμ¬ μμμ μ λ ₯ν΄μ€λλ€. μ¬κΈ°μ Any Appearance, Dark λκ°κ° λ¨λλ°, Dark μμμ λ°λ‘ μ§μ ν κ²½μ°, λΌμ΄νΈλͺ¨λμμ 보μ΄λ ν΄λΉ μκΉ μμμ΄ μ§μ ν μμμΌλ‘ 보μ΄λ, λ€ν¬λͺ¨λκ° λ°λ‘ λμμΈ λμ΄μλ€λ©΄ ν΄λΉ λΆλΆμ μ°Έκ³ νμ¬ μ»¬λ¬μΉ©μ μ§μ νλ κ²μ΄ μ’κ² μ΅λλ€.
λ§μ½, λ€ν¬λͺ¨λλ₯Ό λ°λ‘ λ§λ€μ§ μκ±°λ μμμ΄ λμΌνλ€λ©΄, λκ°μ 컬λ¬μΉ©μ μλμ κ°μ΄ [Shift]ν€λ₯Ό λλ³ λμ μ νν ν μμμ μ§μ νλ©΄ λ©λλ€.
μ¬κΈ°μ 8-bit (0~255)λͺ¨λμ κ²½μ°, κ° RGBκ°μ 0~255κ° λ΄μΈλ‘ μ§μ νλ λ°©μμ΄λ©°, 8-bit Hexadecimalμ 16μ§μ ννμ μμ νκΈ°λ²μ μλ―Έν©λλ€.
μ¬κΈ°μ κ°μ₯ νλ¨μ μλ [Show Color Panel]μ μ ννμ¬ μ§μ μμνλ₯Ό λμΌλ‘ λ³΄κ³ μ νν μ μμ΅λλ€.
μνλ Color Setμ ν΄λ μμ μ 리ν΄λλ©΄ μλμ κ°μ΄, ν΄λΉ ν΄λμμμ Color Setsμ ν λμ λ³Ό μ μμ΅λλ€.
μΈν°νμ΄μ€ λΉλμμ μ§μ μΆκ°ν μμμ μ¬μ©νκΈ°
μλ₯Ό λ€μ΄, UILabelμ μμμ λ³κ²½νκ³ μ νλ€λ©΄ ν΄λΉ λ μ΄λΈμ ν΄λ¦ ν Attribute Inspectorμμ Tint νμ λλ₯΄λ©΄ Named Colors μλμ μκΉ μ§μ μ§μ ν μμλ€μ νμΈν μ μμ΅λλ€. ν΄λ¦ν΄μ μ§μ ν΄μ£Όλ©΄ λ©λλ€. μ¬κΈ°μ iPhone SDK 컬λ¬λ μμ€ν μμ κΈ°λ³Έμ μΌλ‘ μ 곡ν΄μ£Όλ 컬λ¬μ μ λλ€.
μ½λ μμμμ μμ μ κ·ΌνκΈ°
λ§μ½ μ½λ μμμμ λ²νΌ, λ μ΄λΈ λ±μ μμμ μ§μ νκ³ μΆλ€λ©΄ UIColorμ named λ₯Ό ν΅ν΄ μ§μ ν΄λ μμλͺ μ λ¬Έμμ΄λ‘ μ λ¬ν΄μ£Όλ©΄ λ©λλ€.
self.myButton.backgroundColor = UIColor(named: "kakaotalkColor")
λ§μ½ UIColorμ λ©€λ²λ³μλ‘ extension νμ₯νμ¬ μ μΈνμλ€λ©΄ μλμ κ°μ΄ μ½λμμ μ¬μ©ν μ μμ΅λλ€.
self.myButton.backgroundColor = UIColor.kakaotalkColor
// λλ μλμ²λΌ μΈ μ μλ€.
self.myButton.backgroundColor = .kakaotalkColor
ν΄λΉ νλ‘μ νΈ ν΄λ λ΄μ UIColor+Extensionμ΄λΌλ μ΄λ¦μΌλ‘ λ°λ‘ Swift νμΌμ λ§λ€μ΄μ μλμ²λΌ μ½λλ₯Ό μ μΈν΄λλ©΄ μμ κ°μ΄ μ¬μ©ν μ μμ΅λλ€.
// UIColor+Extension.swift
// 1. λ§μ½ Assets ν΄λμ 컬λ¬μ
μ μ§μ νμλ€λ©΄
extension UIColor {
static let myCustomColor = UIColor(named: "myCustomColor")
}
// 2. μ½λλ‘λ§ μμμ μΆκ°ν κ²½μ°
// 2-1. Floating pointλ‘ μμ μ§μ
extension UIColor {
static let myCustomColor = UIColor(
red: 0.776,
green: 0.812,
blue: 0.334
alpha: 1.0
)
}
// 2-2. 8-bit Hexadecimal λ‘ μμ μ§μ
// μνκ°μ΄ μμ κ²½μ° 0xμ΄ν λμ리λ₯Ό λΉΌλ λλ€.
extension UIColor {
static let myCustomColor = UIColor(
argb: 0xFFFFFFFF
)
}
μ°Έκ³ λ§ν¬
https://developer.apple.com/documentation/uikit/uicolor
Apple Developer Documentation
developer.apple.com
Type 'UIColor?' has no member ".myCustomColor" Swift 5.0
I have all my colors in a dedicated color asset. This allows me to define named constants that supports "Any Appearance" and "Dark Appearance". Existing color assets works (previously defined), but...
stackoverflow.com
How to use hex color values
I am trying to use hex color values in Swift, instead of the few standard ones that UIColor allows you to use, but I have no idea how to do it. Example: how would I use #ffffff as a color?
stackoverflow.com
λ§μΉλ©°
μ€λλ μ½μ΄μ£Όμ μ κ°μ¬ν©λλ€.
κΆκΈν μ μ΄ μλ€λ©΄ λκΈλ‘, λμμ΄ λμ ¨λ€λ©΄ κ³΅κ° λΆνλ립λλ€.
νΉμ μμ νκ±°λ νΌλλ°±νμ€ λ΄μ© μλ€λ©΄ μΈμ λ λκΈλ‘ λΆνλ립λλ€.
κ°μ¬ν©λλ€.