| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- //
- // UIFont+Typography.swift
- // DesignKit
- //
- // Created by Jake Lin on 19/10/20.
- //
- import UIKit
- import SwiftUI
- public extension UIFont {
- static let designKit = DesignKitTypography()
- struct DesignKitTypography {
- public var display1: UIFont {
- scaled(baseFont: .systemFont(ofSize: 42, weight: .semibold), forTextStyle: .largeTitle, maximumPointSize: 49)
- }
- public var display2: UIFont {
- scaled(baseFont: .systemFont(ofSize: 36, weight: .semibold), forTextStyle: .largeTitle, maximumPointSize: 42)
- }
- public var title1: UIFont {
- scaled(baseFont: .systemFont(ofSize: 24, weight: .semibold), forTextStyle: .title1, maximumPointSize: 29)
- }
- public var title2: UIFont {
- scaled(baseFont: .systemFont(ofSize: 20, weight: .semibold), forTextStyle: .title2, maximumPointSize: 24)
- }
- public var title3: UIFont {
- scaled(baseFont: .systemFont(ofSize: 18, weight: .semibold), forTextStyle: .title3, maximumPointSize: 23)
- }
- public var title4: UIFont {
- scaled(baseFont: .systemFont(ofSize: 14, weight: .regular), forTextStyle: .headline, maximumPointSize: 18)
- }
- public var title5: UIFont {
- scaled(baseFont: .systemFont(ofSize: 12, weight: .regular), forTextStyle: .subheadline, maximumPointSize: 16)
- }
- public var bodyBold: UIFont {
- scaled(baseFont: .systemFont(ofSize: 16, weight: .semibold), forTextStyle: .body, maximumPointSize: 21)
- }
- public var body: UIFont {
- scaled(baseFont: .systemFont(ofSize: 16, weight: .regular), forTextStyle: .body, maximumPointSize: 21)
- }
- public var captionBold: UIFont {
- scaled(baseFont: .systemFont(ofSize: 14, weight: .semibold), forTextStyle: .caption1, maximumPointSize: 20)
- }
- public var caption: UIFont {
- scaled(baseFont: .systemFont(ofSize: 14, weight: .light), forTextStyle: .caption1, maximumPointSize: 20)
- }
- public var small: UIFont {
- scaled(baseFont: .systemFont(ofSize: 12, weight: .light), forTextStyle: .footnote, maximumPointSize: 16)
- }
- }
- static func font(ofSize fontSize: CGFloat, weight: UIFont.Weight = .regular) -> UIFont {
- return .systemFont(ofSize: fontSize, weight: weight)
- }
- }
- public extension Font {
- static func font(ofSize fontSize: CGFloat, weight: UIFont.Weight = .regular) -> Font {
- return Font(UIFont.systemFont(ofSize: fontSize, weight: weight))
- }
- }
- private extension UIFont.DesignKitTypography {
- func scaled(baseFont: UIFont, forTextStyle textStyle: UIFont.TextStyle = .body, maximumPointSize: CGFloat? = nil) -> UIFont {
- let fontMetrics = UIFontMetrics(forTextStyle: textStyle)
- if let maximumPointSize = maximumPointSize {
- return fontMetrics.scaledFont(for: baseFont, maximumPointSize: maximumPointSize)
- }
- return fontMetrics.scaledFont(for: baseFont)
- }
- }
|