Search code examples
swiftnsattributedstring

Is it possible to set line height less than 1 in Swift?


I am trying to make the line space a little less than the default for a small window.

I have code similar to this question: How to Increase Line spacing in UILabel in Swift

let title = "This is text that will be long enough to form two lines"
let styles = NSMutableParagraphStyle()
styles.lineSpacing = 0.1
let attribs = [
    NSAttributedString.Key.paragraphStyle:styles
]
let attrString:NSAttributedString = NSAttributedString.init(string: title, attributes: attribs)
introText.attributedStringValue = attrString

While changing lineSpacing to 10 makes a noticeable difference, I can't see a difference if I make it less than 1.

Here is what 0.1 looks like:

enter image description here


Solution

  • Line spacing is measured in points, not lines. There's basically no such thing as a fraction of a point (for drawing purposes; I am simplifying, since retina screens do exist). Zero is the minimum, and when you say 0.1, you are there; you can't reduce the leading any further.