Skip to content

Instantly share code, notes, and snippets.

@jkereako
Created September 7, 2020 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkereako/d4bb20661d28ff1e5891ec10bdb76f87 to your computer and use it in GitHub Desktop.
Save jkereako/d4bb20661d28ff1e5891ec10bdb76f87 to your computer and use it in GitHub Desktop.
Example of using an Swift enum as a factory.
enum Font {
enum Style: String {
case bold = "Bold"
case regular = "Regular"
}
case fontAwesome(size: CGFloat)
case roboto(size: CGFloat, style: Style)
case robotoCondensed(size: CGFloat, style: Style)
var uiFont: UIFont {
switch self {
case .fontAwesome(let size):
return UIFont(name: "FontAwesome", size: size)!
case .roboto(let size, let style):
return UIFont(name: "Roboto-\(style.rawValue)", size: size)!
case .robotoCondensed(let size, let style):
return UIFont(name: "RobotoCondensed-\(style.rawValue)", size: size)!
}
}
}
@jkereako
Copy link
Author

jkereako commented Sep 7, 2020

Usage

Font.roboto(size: 17, style: .regular).uiFont

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment