Search code examples
iosswiftswiftuifontsformatting

How to get the default LineSpacing of a font in SwiftUI?


I wanted to get the line spacing of a font in SwiftUI. Something like this:

let font = UIFont.preferredFont(forTextStyle: .body)
font.lineSpacing  // << not possible

I am able to get the pointSize and lineHeight but don't know how to calculate the line spacing. It seems to be 2 when I tried to figure it out visually but I'm not sure.

I also tried using @Environment(\.lineSpacing) var spacing but this always returned 0. Not sure how this is used.

How can I find the line spacing of a particular font (for example, the default .body font)?


Solution

  • The .leading property is what gives line spacing.

    UIFont.preferredFont(forTextStyle: .body).leading  // returns ~1.712