Skip to content

Instantly share code, notes, and snippets.

@arashkashi
Created March 15, 2022 19:44
Show Gist options
  • Save arashkashi/b5d79ab4e93e42c2eb557ed00770df05 to your computer and use it in GitHub Desktop.
Save arashkashi/b5d79ab4e93e42c2eb557ed00770df05 to your computer and use it in GitHub Desktop.
custom fonts wrapped into the same style structure of Apple (/// https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/typography/)
enum TintFont {
enum TypeFace {
case bold
case boldItalic
case book
case bookItalic
case medium
case mediumItalic
func getFont(tint: TintFont) -> Font {
var result: Font?
switch self {
case .bold:
if let valid = UIFont(name: "CircularStd-Bold", size: tint.size) {
result = Font(valid)
}
case .boldItalic:
if let valid = UIFont(name: "CircularStd-BoldItalic", size: tint.size) {
result = Font(valid)
}
case .book:
if let valid = UIFont(name: "CircularStd-Book", size: tint.size) {
result = Font(valid)
}
case .bookItalic:
if let valid = UIFont(name: "CircularStd-BookItalic", size: tint.size) {
result = Font(valid)
}
case .medium:
if let valid = UIFont(name: "CircularStd-Medium", size: tint.size) {
result = Font(valid)
}
case .mediumItalic:
if let valid = UIFont(name: "CircularStd-MediumItalic", size: tint.size) {
result = Font(valid)
}
}
return result == nil ? tint.getFallBackFont() : result!.weight(tint.weight)
}
}
case largeTitle
case title
case title2
case title3
case headline
case subheadline
case body
case callout
case footnote
case caption
case caption2
/// This functions returns the system font equivalent of the design fonts,
/// this fall back process manages the situation where the font may have not been loaded properly.
/// - Returns: Font
private func getFallBackFont() -> Font {
switch self {
case .largeTitle:
return Font.largeTitle
case .title:
return Font.title
case .title2:
return Font.title2
case .title3:
return Font.title3
case .headline:
return Font.headline
case .subheadline:
return Font.subheadline
case .body:
return Font.body
case .callout:
return Font.callout
case .footnote:
return Font.footnote
case .caption:
return Font.caption
case .caption2:
return Font.caption
}
}
var font: Font {
switch self {
case .headline:
return TintFont.TypeFace.bold.getFont(tint: self)
default:
return TintFont.TypeFace.medium.getFont(tint: self)
}
}
/// https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/typography/
var size: CGFloat {
switch self {
case .largeTitle:
return 34.0
case .title:
return 28.0
case .title2:
return 22.0
case .title3:
return 20.0
case .headline:
return 17.0
case .subheadline:
return 15.0
case .body:
return 17.0
case .callout:
return 16.0
case .footnote:
return 13.0
case .caption:
return 12.0
case .caption2:
return 11.0
}
}
var weight: Font.Weight {
switch self {
case .headline:
return .semibold
default:
return .regular
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment