定义核心文本段落而不使用换行符?

发布于 2025-01-06 21:45:53 字数 1342 浏览 0 评论 0原文

我在为 NSAttributedString 中的段落应用适当的顶部和底部间距时遇到了一个小问题。我使用此代码来设置段落属性:

int sf = sizeof(CGFloat);
        CTParagraphStyleSetting settings[ParagraphStylesSupported] = 
            {
                { kCTParagraphStyleSpecifierAlignment, sizeof(QuartzTextAlignment), &style.textAlignment },
                { kCTParagraphStyleSpecifierParagraphSpacingBefore, sf, &marginTop},
                { kCTParagraphStyleSpecifierParagraphSpacing, sf, &marginBot},
                { kCTParagraphStyleSpecifierMinimumLineHeight, sf, &lineHeight},

                { kCTParagraphStyleSpecifierLineSpacing, sf, &lineSpacing},
                { kCTParagraphStyleSpecifierFirstLineHeadIndent, sf, &style.firstLineIndent},

            };

            CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, ParagraphStylesSupported);


            [string addAttribute:(NSString*)kCTParagraphStyleAttributeName value:(id)paragraphStyle range:item.range];
            CFRelease(paragraphStyle);

文本属性正在按预期应用。 但是段落对齐几乎没有问题:

  1. 除非属性范围的开头有换行符 \n ,否则段落不会一个接一个地放置,而是“内联”流动。
  2. 当我添加换行符时,段落将正确地一个一个地放置在另一个段落下面,但换行行高度将添加到间距“ParagraphSpacing”间隙中。
  3. kCTParagraphStyleSpecifierParagraphSpacingBefore 还会影响段落范围内的换行符。

什么 Core Text 布局引擎将其解释为段落指示符? 是属性字符串中的每个换行符吗?

I am having a trivial problem with applying proper top and bottom spacing for the paragraphs in NSAttributedString. I am usng this code to set paragraph attributes:

int sf = sizeof(CGFloat);
        CTParagraphStyleSetting settings[ParagraphStylesSupported] = 
            {
                { kCTParagraphStyleSpecifierAlignment, sizeof(QuartzTextAlignment), &style.textAlignment },
                { kCTParagraphStyleSpecifierParagraphSpacingBefore, sf, &marginTop},
                { kCTParagraphStyleSpecifierParagraphSpacing, sf, &marginBot},
                { kCTParagraphStyleSpecifierMinimumLineHeight, sf, &lineHeight},

                { kCTParagraphStyleSpecifierLineSpacing, sf, &lineSpacing},
                { kCTParagraphStyleSpecifierFirstLineHeadIndent, sf, &style.firstLineIndent},

            };

            CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, ParagraphStylesSupported);


            [string addAttribute:(NSString*)kCTParagraphStyleAttributeName value:(id)paragraphStyle range:item.range];
            CFRelease(paragraphStyle);

Text properties are being applied as expected.
But there are few problems with paragraph alignment:

  1. Paragraphs are not being places after each other but flow 'inline' unless there is a newline \n character at the beginning of the attribute range.
  2. When I add the newline character, then paragraphs are being placed correctly one below another, but newline line height is being added to the spacing 'ParagraphSpacing' gaps.
  3. kCTParagraphStyleSpecifierParagraphSpacingBefore affects also the newlines character inside the paragraph range.

What Core Text layout engine interprets as a paragraph indicator?
Is it every newline character in the attributed string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的鱼塘能养鲲 2025-01-13 21:45:53

在核心文本中,换行符 (\n) 结束一个段落。我认为没有办法告诉它使用不同的字符。

如果您连续放置两个换行符,那么核心文本将表现得好像您有一个空段落,因此它将添加额外的间距。

确保每个段落后只有一个换行符。如果段落之间的间距仍然太大,则需要为 kCTParagraphStyleSpecifierParagraphSpacingBeforekCTParagraphStyleSpecifierParagraphSpacing(或两者)使用较小的值。

In Core Text, the newline character (\n) ends a paragraph. I don't think there is a way to tell it to use a different character.

If you put two newlines in a row, then Core Text will act as if you have an empty paragraph, so it will put in extra spacing.

Make sure you only have one newline after each paragraph. If your paragraphs still have too much space between them, you need to use a smaller value for kCTParagraphStyleSpecifierParagraphSpacingBefore or kCTParagraphStyleSpecifierParagraphSpacing (or both).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文