NSAttributedString 中的段落是如何定义的?

发布于 2024-10-06 22:13:50 字数 324 浏览 0 评论 0原文

我看到有一个属性名称(NSParagraphStyleAttributeName)用于将段落样式应用于 Cocoa 中的文本。文本存储在 NSAttributedString 中,但是该字符串中的“段落”是由什么定义的——是换行符 \n 吗?其他角色?如果它是 \n,那么如何在不开始新段落的情况下创建新行。最后,当您将 ParagraphStyle 附加到字符串时,是否必须使用整个段落的确切范围,或者可以将其放在段落中的任何子范围上。如果它可以是一个子范围,系统如何处理同一段落上的两个或多个 ParagraphStyle?

谢谢, 抢

I see that there is an attribute name (NSParagraphStyleAttributeName) for applying paragraph styles to text in Cocoa. Text is stored in an NSAttributedString, but what defines a "paragraph" in that string -- is it the newline character \n? Other characters? If it's a \n, then how do you create a new line without starting a new paragraph. And lastly, when you attach a ParagraphStyle to the string, do you have to use the exact range of the whole paragraph, or can you put it on any sub-range in the paragraph. If it can be a sub-range, how does the system handle two or more ParagraphStyles on the same paragraph?

thanks,
Rob

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

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

发布评论

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

评论(3

半透明的墙 2024-10-13 22:13:50

我在苹果的 cocoa-dev 邮件列表上得到了 Douglas 的答复:

http://lists。 apple.com/archives/Cocoa-dev/2010/Dec/msg00347.html

我将复制他在这里写的内容:

可以使用任何标准段落分隔符(\n、\r、\r\n ,Unicode 段落分隔符)。使用 Unicode 行分隔符开始新行,无需分段。最好将段落样式应用于整个段落;如果不这样做,那么段落样式属性将在属性固定时自动固定,以便它在每个段落范围内保持不变,因为这是在布局时需要的。

I got an answer from Douglas on Apple's cocoa-dev mailing list:

http://lists.apple.com/archives/Cocoa-dev/2010/Dec/msg00347.html

I'll copy what he wrote here:

Any of the standard paragraph separators can be used (\n, \r, \r\n, Unicode paragraph separator). Use the Unicode line separator character to start a new line without a paragraph break. It is best to apply a paragraph style to an entire paragraph; if that is not done, then the paragraph style attribute will be automatically fixed at attribute fixing time so that it is constant over each paragraph range, because that is needed at layout time.

ˉ厌 2024-10-13 22:13:50

可能值得一提的是,Apple 的字符串编程指南有一个关于段落和换行符的部分,并且 NSString 将为您提供段落范围,而无需搜索段落分隔符。

It might be worth mentioning that Apple's String Programming Guide has a section on Paragraph and Line Breaks, and that NSString will give you the paragraph ranges without your having to search for paragraph separators.

攒一口袋星星 2024-10-13 22:13:50

根据我的经验,iOS 中最好的方法是使用

@"\n\r";

我发现与仅使用 \n 甚至 0x2029 不一致的行为(这应该相当于 NSParagraphSeparatorCharacter (未在 iOS 中定义) ))

使用 NSAttributedStringNSParagraphStyle 时出现问题...就像使用 @"\n" 或 (0x2029) 时 setParagraphSpacing 被第一段以外的其他段落忽略一样...使用 @"\n\r" 得到了正确的段落

the best way in iOS from my experience is to use

@"\n\r";

i found inconsistent behaviour with just using \n or even 0x2029 (which should be the equivalent of NSParagraphSeparatorCharacter (not defined in iOS))

the problems emerged when using NSAttributedString and NSParagraphStyle... as when using the @"\n" or (0x2029) the setParagraphSpacing was ignored by other than the first paragraph... using @"\n\r" got the paragraphes right

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