5
3

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 Color LiteralはGitでどう表現されるか

Posted at

はじめに

Xcode 8でSwift Color Literalが本格的に導入されました。Xcode 7.1のplaygroundですでに採用されていましたが、それ以外でも利用できるようになりました。
Color Literalは、直感的に色(iOS系のUIColorとmacOS系のNSColor)を指定できる記法です。(CGColorの場合は、[色].CGColorのような形で指定します。)

入力方法

XcodeScreenSnapz001.png

Edit > Format > Show Colorsと選択すると、上のスクリーンショットのようにColorsパレットが表示されます。色をコード上にドラッグドロップすると、色をColor Literalとして入力できます。

Color Literalをダブルクリックすると、再編集できます。

Git上でどう表現されるか

文字列化されると、こういう形になります。RGBとアルファをdouble型の0〜1の実数で表現しています。

view.backgroundColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)

関連リンク

UIColorをColor Literalで定義する
Literals in Playgrounds
UIColorの定義はすべて#colorLiteral使ってみることにしてみた
コードの実例 https://github.com/hsylife/HowToDisplayPopover/blob/master/HowToDisplayPopover/ContentViewController.swift

5
3
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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?