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: 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 its params.