4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift:UIColorの色指定に独自色を追加する

Posted at

UIColorにはUIColor.redやらUIColor.darkGrayなどの便利な色指定がデフォルトで存在しますが,独自の色を追加する方法をまとめておきます.

Color Assetとして色を追加する(非必須)

Asset Catalogで色を管理できます.最近はダークモードへの対応などもあると思うので,ぜひ使いましょう.
color.png
↑こんな感じ

UIColorのExtensionを記述する

extension UIColor {
    static let 色名 = UIColor() //お好きな初期化
    // 例
    static let danger = UIColor(named: "dangerColor")! //Asset Catalogに登録した名前
}

これだけでどこからでもUIColor.dangerなどで色を使うことができるようになります.(Asset Catalogを使うと微妙な色の調整もまとめて管理できますね)

#備考
NSColorColor Assetを使う場合は,NSColor(named: NSColor.Name(""))!という風にNSColor.Nameを噛ませる必要があります.

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?