Search code examples
iosobjective-cuitextviewnsattributedstringtextkit

Why does the second text block have so much higher line height than the first despite NSAttributedString disagreeing?


I'm given one NSAttributedString with two parts of text in it, the first is just a plain body of text, shown below, and after that is a sample block of code, also shown below (added a divider to more easily show).

As you can see, the block of code has significantly taller lines than the normal body of text:

enter image description here

However, there's no extraneous newlines or anything of the sort. This is the attributedString.string value for it:

func sampleFunction() -> Bool {
    if 4 + 4 == 8 {
        return true
    }

    return false
}

There are both in the same NSAttributedString, and when I print the NSAttributedString I can spot no differences that would account for this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed lacinia, ex ut placerat lobortis, nunc metus convallis nibh, a laoreet velit nisi non felis. Ut vitae quam in ipsum vestibulum gravida et ut nisl.
Vestibulum vel enim vitae nunc dignissim posuere. Morbi sed ipsum odio. Cras laoreet nec ligula ac lobortis. Curabitur a tristique nulla. Proin et massa purus. Vestibulum et volutpat mi.
{
    NSColor = "UIExtendedGrayColorSpace 0 1";
    NSFont = "<UICTFont: 0x7fd019e1b9f0> font-family: \".SFUIText\"; font-weight: normal; font-style: normal; font-size: 15.00pt";
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 15, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 1, LineBreakMode 0, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection -1, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
}func sampleFunction() -> Bool {
    if 4 + 4 == 8 {
        return true
    }

    return false
}{
    NSColor = "UIExtendedGrayColorSpace 0 1";
    NSFont = "<UICTFont: 0x7fd019f128f0> font-family: \"Menlo-Regular\"; font-weight: normal; font-style: normal; font-size: 15.00pt";
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 15, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 1, LineBreakMode 0, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection -1, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
}

I cannot for the life of me spot what would be causing this. It's simply displayed in a UITextView, but why are the code lines so much taller?


Solution

  • The reason the code text looks so spread out is because of the NSParagraphStyle.paragraphSpacing. Since each of the code lines ends with a newline, each line is interpreted as its own paragraph, which accounts for the space.