Skip to content

Instantly share code, notes, and snippets.

@ferologics
Created December 23, 2021 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferologics/94f6575b02faa39dcab9f4a028782751 to your computer and use it in GitHub Desktop.
Save ferologics/94f6575b02faa39dcab9f4a028782751 to your computer and use it in GitHub Desktop.
this example uses Dynamic Font with respect to line height line height
// MARK: - Helper
struct Layout { // this uses Dynamic Font and calculates spacing for correct line height
static let weight = SwiftUI.Font.Weight.semibold
static let size = UIFontMetrics.default.scaledValue(for: 17)
static let lineHeight = UIFontMetrics.default.scaledValue(for: 22)
static let uiFont = UIFont.systemFont(ofSize: size, weight: weight.toUIKit())
static let spacing = max(0, lineHeight - uiFont.lineHeight)
}
// MARK: - Usage
struct ContentView: View {
var body: some View {
Text("Hello nerd")
.font(.system(size: Layout.size, weight: Layout.weight))
.lineSpacing(Layout.lineHeight)
.padding(.vertical, Layout.spacing/2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment