Soheil Novinfard
Soheil Novinfard

Reputation: 1444

How to calculate line spacing based on font's line height in Swift?

You can set the line spacing for attributed text in labels by the following code:

let attributedString = NSMutableAttributedString(string: "My text")

let paragraphStyle = NSMutableParagraphStyle()

// line spacing in points
paragraphStyle.lineSpacing = 1.5

attributedString.addAttribute(NSParagraphStyleAttributeName,
 value:paragraphStyle, range:NSMakeRange(0, attributedString.length))


label.attributedText = attributedString

But whenever I check the design, I face something named Line Height for font which is in different scale, for example:

Font Size: 14pt
Line Height: 18pt

How can I convert the number to paragraph line spacing?

Upvotes: 2

Views: 6140

Answers (1)

Soheil Novinfard
Soheil Novinfard

Reputation: 1444

I found the answer. You can calculate the line spacing as follows:

Line Spacing = (Line Height - Font Size) / 2

Upvotes: 4

Related Questions