ios - 尽管 NSAttributedString 不同意,为什么第二个文本 block 的行高比第一个高得多?

标签 ios objective-c uitextview nsattributedstring textkit

我得到一个包含两部分文本的 NSAttributedString,第一部分只是纯文本,如下所示,之后是示例代码块,也如下所示(添加了分隔线以更轻松地显示)。

如您所见,代码块的行数明显高于正常的文本正文:

enter image description here

但是,没有多余的换行符或任何类似的东西。这是它的 attributedString.string 值:

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

    return false
}

两者都在同一个 NSAttributedString 中,当我打印 NSAttributedString 时,我没有发现任何可以解释这一点的差异:

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";
}

我这辈子都无法找出是什么原因造成的。它只是简单地显示在一个 UITextView 中,但是为什么代码行那么高?

最佳答案

代码文本看起来如此分散的原因是 NSParagraphStyle.paragraphSpacing。由于每个代码行都以换行符结尾,因此每一行都被解释为它自己的段落,它占了空间。

关于ios - 尽管 NSAttributedString 不同意,为什么第二个文本 block 的行高比第一个高得多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42077907/

相关文章:

ios - 为什么我的 iPhone 无法从 Apple Watch 获取心率数据,但 watch 扩展可以?

iphone - Objective C ivars 或 @property

ios - 将组件放置在 UITextView 下方并调整大小以适合

iPhone 键盘在进入时隐藏了一个 UITextView?

ios - 从 Xcode 11.1 升级到 Xcode 11.2 后,应用程序因 _UITextLayoutView 崩溃

ios - 从 XCTestCase 子类访问 App Delegate - 类型错误?

ios - 错误消息 '_BSMachError: (os/kern) invalid capability (20)'

ios - apple tv 支持音量控制吗?

ios - 序列中可以有 if 语句吗?

objective-c - 你怎么称呼在有条件的情况下做作业?