Skip to content

Instantly share code, notes, and snippets.

@OleksiiShvachenko
Created March 18, 2019 09:51
Font manager for fonts generated by SwiftGen
import UIKit
enum FontStyle {
case largeTitle
case headline
case title2
case title3
case subheadline
case body
case callout
case footnote
case caption1
case caption2
var font: UIFont {
switch self {
case .largeTitle:
return FontFamily.Plain.light.font(for: .largeTitle)
case .headline:
return FontFamily.Plain.medium.font(for: .headline)
case .title2:
return FontFamily.Plain.light.font(for: .title2)
case .title3:
return FontFamily.Plain.medium.font(for: .title3)
case .subheadline:
return FontFamily.Plain.medium.font(for: .subheadline)
case .body:
return FontFamily.Plain.light.font(for: .body)
case .callout:
return FontFamily.Plain.medium.font(for: .callout)
case .footnote:
return FontFamily.Plain.light.font(for: .footnote)
case .caption1:
return FontFamily.Plain.light.font(for: .caption1)
case .caption2:
return FontFamily.Plain.light.font(for: .caption2)
}
}
}
fileprivate extension FontConvertible {
func font(for style: UIFont.TextStyle) -> UIFont {
guard let size = FontConvertible.styles[style] else {
fatalError()
}
return font(size: size)
// Remove comments when accessability feature is added
// let fontMetrics = UIFontMetrics(forTextStyle: textStyle)
// return fontMetrics.scaledFont(for: font)
}
private static var styles: [UIFont.TextStyle: CGFloat] {
return [
.largeTitle: 34,
.title1: 34,
.title2: 10,
.title3: 19,
.headline: 17,
.subheadline: 15,
.body: 17,
.callout: 16,
.footnote: 15,
.caption1: 13,
.caption2: 11
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment