如何根据Swift中字体的行高计算行距?

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

您可以通过以下代码设置标签中属性文本的行距:

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

但是每当我检查设计时,我都会遇到一些名为 Line Height 的不同比例的字体,例如:

Font Size: 14pt
Line Height: 18pt

如何将数字转换为段落行距?

我找到了答案。您可以按如下方式计算行距:

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