NSAttributedString 中的段落是如何定义的?
我看到有一个属性名称(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在苹果的 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.
可能值得一提的是,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.
根据我的经验,iOS 中最好的方法是使用
我发现与仅使用
\n
甚至0x2029
不一致的行为(这应该相当于 NSParagraphSeparatorCharacter (未在 iOS 中定义) ))使用
NSAttributedString
和NSParagraphStyle
时出现问题...就像使用 @"\n" 或 (0x2029) 时 setParagraphSpacing 被第一段以外的其他段落忽略一样...使用@"\n\r"
得到了正确的段落the best way in iOS from my experience is to use
i found inconsistent behaviour with just using
\n
or even0x2029
(which should be the equivalent of NSParagraphSeparatorCharacter (not defined in iOS))the problems emerged when using
NSAttributedString
andNSParagraphStyle
... as when using the @"\n" or (0x2029) the setParagraphSpacing was ignored by other than the first paragraph... using@"\n\r"
got the paragraphes right