從 Assets 設定 App 使用的顏色
Published in
Jul 3, 2018
Assets 現在除了加入圖片,還可以加入有名字的顏色,方便我們之後在 Attributes inspector 或程式裡使用,方法如下:
點選 Assets,在頁面的左邊區塊,點選下方的 +,選擇 Color Set 新增顏色
此時 Assets 裡將新增代表顏色的資料,它的名字叫 Color。
ps: 我們也可以在左邊區塊按右鍵選單,然後選擇 New Color Set 新增顏色。
顏色預設會有兩個格子,其中的 Dark Appearance 代表 dark mode 的顏色。如果不想設定 dark mode,可從 Attributes inspector 將 Appearances 設為 None。
此時將變成只有一個格子。
設定顏色的名字
點選顏色的名字後可修改它的名字,以下我們輸入黛綠。
感謝中國傳統色彩大全提供許多有趣的中國傳統顏色名字。
https://kknews.cc/zh-tw/home/jr3n5ny.html
點選顏色的格子,於右邊的 Attributes inspector 頁面設定顏色
從 Input Method 可調整顏色的輸入方法。
8-bit Hexadecimal : 十六進位
8-bit (0–255): 十進位
完成像林黛玉的黛綠色設定後,之後即可在 Attributes inspector 或程式裡使用。
從 Attributes inspector 設定顏色
- SwiftUI
- storyboard
從程式設定顏色
- SwiftUI
寫法 1
Text("Hello, world!")
.foregroundStyle(.黛綠)
寫法 2
Text("Hello, world!")
.foregroundStyle(Color(.黛綠))
寫法 3
Text("Hello, world!")
.foregroundStyle(Color("黛綠"))
- UIKit
寫法 1
view.backgroundColor = .黛綠
寫法 2
view.backgroundColor = UIColor(resource: .黛綠)
寫法 3
view.backgroundColor = UIColor(named: "黛綠")