Back to Swiftgen

Swift4

Documentation/templates/colors/swift4.md

6.6.32.3 KB
Original Source

Template Information

NameDescription
File namecolors/swift4.stencil
Configuration example<pre>colors:
inputs: path/to/colors-file.txt
outputs:
templateName: 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.

Customization

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

Parameter NameDefault ValueDescription
colorAliasNameColorAllows you to change the name of the generated typealias for the platform specific color type.
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.

Generated Code

Extract:

swift
internal struct ColorName {
  internal let rgbaValue: UInt32
  internal var color: Color { return Color(named: self) }

  /// <span style="display:block;width:3em;height:2em;border:1px solid black;background:#339666"></span>
  /// Alpha: 100% 
 (0x339666ff)
  internal static let articleBody = ColorName(rgbaValue: 0x339666ff)
  /// <span style="display:block;width:3em;height:2em;border:1px solid black;background:#ff66cc"></span>
  /// Alpha: 100% 
 (0xff66ccff)
  internal static let articleFootnote = ColorName(rgbaValue: 0xff66ccff)
}

Full generated code

Usage example

swift
// You can create colors with the convenience constructor like this:
let title = UIColor(named: .articleBody)
let footnote = UIColor(named: .articleFootnote)

// Or as an alternative, you can refer to enum instance and call .color on it:
let sameTitle = ColorName.articleBody.color
let sameFootnote = ColorName.articleFootnote.color