Back to Swiftgen

Literals Swift4

Documentation/templates/colors/literals-swift4.md

6.6.32.1 KB
Original Source

Template Information

NameDescription
File namecolors/literals-swift4.stencil
Configuration example<pre>colors:
inputs: path/to/colors-file.txt
outputs:
templateName: literals-swift4
output: Colors.swift</pre> |

| Language | Swift 4 | | Author | Olivier Halligon |

When to use it

  • When you need to generate Swift 4 code.
  • Supports multiple color names with the same value.
  • Uses #colorLiterals for easy preview of the actual color.

Customization

You can customize some elements of this template by overriding the following parameters when invoking swiftgen. See the dedicated documentation.

Parameter NameDefault ValueDescription
enumNameColorNameAllows you to change the name of the generated enum containing all colors.
forceFileNameEnumN/ASetting this parameter will generate an enum <FileName> even if only one FileName was provided as input.
publicAccessN/AIf set, the generated constants will be marked as public. Otherwise, they'll be declared internal.

Note: if you use enumName: UIColor (or NSColor on macOS) then the color constants will be generated as an extension of the UIColor (iOS) / NSColor (macOS) type directly without creating a separate enum type for namespacing those color constants.

Generated Code

Extract:

swift
internal extension ColorName {
  /// 0x339666ff (r: 51, g: 150, b: 102, a: 255)
  static let articleBody = #colorLiteral(red: 0.2, green: 0.588235, blue: 0.4, alpha: 1.0)
  /// 0xff66ccff (r: 255, g: 102, b: 204, a: 255)
  static let articleFootnote = #colorLiteral(red: 1.0, green: 0.4, blue: 0.8, alpha: 1.0)
}

Full generated code

Usage example

swift
// To reference a color, simpy reference its static instance by name:
let title = ColorName.articleBody
let footnote = ColorName.articleFootnote