UIFontExtensions.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // UIFont+Typography.swift
  3. // DesignKit
  4. //
  5. // Created by Jake Lin on 19/10/20.
  6. //
  7. import UIKit
  8. import SwiftUI
  9. public extension UIFont {
  10. static let designKit = DesignKitTypography()
  11. struct DesignKitTypography {
  12. public var display1: UIFont {
  13. scaled(baseFont: .systemFont(ofSize: 42, weight: .semibold), forTextStyle: .largeTitle, maximumPointSize: 49)
  14. }
  15. public var display2: UIFont {
  16. scaled(baseFont: .systemFont(ofSize: 36, weight: .semibold), forTextStyle: .largeTitle, maximumPointSize: 42)
  17. }
  18. public var title1: UIFont {
  19. scaled(baseFont: .systemFont(ofSize: 24, weight: .semibold), forTextStyle: .title1, maximumPointSize: 29)
  20. }
  21. public var title2: UIFont {
  22. scaled(baseFont: .systemFont(ofSize: 20, weight: .semibold), forTextStyle: .title2, maximumPointSize: 24)
  23. }
  24. public var title3: UIFont {
  25. scaled(baseFont: .systemFont(ofSize: 18, weight: .semibold), forTextStyle: .title3, maximumPointSize: 23)
  26. }
  27. public var title4: UIFont {
  28. scaled(baseFont: .systemFont(ofSize: 14, weight: .regular), forTextStyle: .headline, maximumPointSize: 18)
  29. }
  30. public var title5: UIFont {
  31. scaled(baseFont: .systemFont(ofSize: 12, weight: .regular), forTextStyle: .subheadline, maximumPointSize: 16)
  32. }
  33. public var bodyBold: UIFont {
  34. scaled(baseFont: .systemFont(ofSize: 16, weight: .semibold), forTextStyle: .body, maximumPointSize: 21)
  35. }
  36. public var body: UIFont {
  37. scaled(baseFont: .systemFont(ofSize: 16, weight: .regular), forTextStyle: .body, maximumPointSize: 21)
  38. }
  39. public var captionBold: UIFont {
  40. scaled(baseFont: .systemFont(ofSize: 14, weight: .semibold), forTextStyle: .caption1, maximumPointSize: 20)
  41. }
  42. public var caption: UIFont {
  43. scaled(baseFont: .systemFont(ofSize: 14, weight: .light), forTextStyle: .caption1, maximumPointSize: 20)
  44. }
  45. public var small: UIFont {
  46. scaled(baseFont: .systemFont(ofSize: 12, weight: .light), forTextStyle: .footnote, maximumPointSize: 16)
  47. }
  48. }
  49. static func font(ofSize fontSize: CGFloat, weight: UIFont.Weight = .regular) -> UIFont {
  50. return .systemFont(ofSize: fontSize, weight: weight)
  51. }
  52. }
  53. public extension Font {
  54. static func font(ofSize fontSize: CGFloat, weight: UIFont.Weight = .regular) -> Font {
  55. return Font(UIFont.systemFont(ofSize: fontSize, weight: weight))
  56. }
  57. }
  58. private extension UIFont.DesignKitTypography {
  59. func scaled(baseFont: UIFont, forTextStyle textStyle: UIFont.TextStyle = .body, maximumPointSize: CGFloat? = nil) -> UIFont {
  60. let fontMetrics = UIFontMetrics(forTextStyle: textStyle)
  61. if let maximumPointSize = maximumPointSize {
  62. return fontMetrics.scaledFont(for: baseFont, maximumPointSize: maximumPointSize)
  63. }
  64. return fontMetrics.scaledFont(for: baseFont)
  65. }
  66. }