Improving UIFont workflow

:  ~ 45 sec read

Usually an app has fonts with well defined purposes. So why not let enums make our lives easier, a little bit? First, a couple of them, to define our font families and weights:

struct Font {
  private enum Family: String {
    case avenirNext
    case proximaNova 
  }
  
  private enum Weight: String {
    case regular
    case medium
    case demiBold
    case bold
  }
}

Then a method to easily create fonts:

private static func baseFont(family: Family, size: CGFloat, weight: Weight = .regular, italic: Bool = false) -> UIFont {
  let font = family.rawValue
  let modifier = weight.rawValue + (italic ? "Italic" : "")
  return UIFont(name: "\(font)-\(modifier)", size: size)!
}

Finally, the properties:

static let barButton = Font.baseFont(family: .avenirNext, size: 16, weight: .medium)
static let header    = Font.baseFont(family: .proximaNova, size: 18, weight: .demiBold)

If the app has only one font family, everything becomes even simpler, by removing Family and the related params.