Anton Lovchikov
Anton Lovchikov

Reputation: 523

How to decrease the lineSpacing?

I have an Attributed string and I want to decrease the standard line height of it. To do so, I need to set a negative lineSpacing to my NSMutableParagraphStyle. But it's illegal according to Apple's docs.

Fun fact is that the negative lineSpacing actually works, but causes an extra bottom spacing in the UILabel which depends on the number of lines.

enter image description here

Is it possible to decrease the line height without having side effects?

Upvotes: 2

Views: 2039

Answers (1)

Cameron Porter
Cameron Porter

Reputation: 939

Use NSParagraphStyle.lineHightMultiple https://developer.apple.com/documentation/uikit/nsparagraphstyle/1528614-lineheightmultiple

You can set the lineHeightMultiple to a value greater than 0 but less than 1 and it'll reduce line spacing.

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = 0.83 //Try different values here to see what looks best

let attrString = NSMutableAttributedString(string: "Your string")
attrString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attrString.length))

You can also do this from storyboard:

enter image description here

Upvotes: 3

Related Questions