将 NSAttributedString 转换为纯文本
我有一个 NSData
实例,其中包含源自 NSTextView
的属性文本 (NSAttributedString
)。我想将属性字符串转换为纯字符串 (NSString
),无需任何格式来进行一些文本分析(在转换时,我无法访问原始 NSTextView 或其 NSTextStorage 实例)。
最好的方法是什么?
编辑:
出于好奇,我检查了以下结果:
[[[self textView] textStorage] words]
这对于进行一些文本分析来说似乎很方便。生成的数组包含 NSSubTextStorage 的实例(下面是单词“Eastern”的示例):
东方{ NSFont = "\"LucidaGrande 11.00 pt. P [] (0x7ffcaae08330) fobj=0x10a8472d0, spc=3.48\""; NSParagraphStyle = "对齐方式 0,行间距 0,段落间距 0,段落间距之前 0,头缩进 0,尾缩进 0, FirstLineHeadIndent 0、LineHeight 0/0、LineHeightMultiple 0、 换行模式 0、制表符 (\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 0、块(空)、列表(空)、 BaseWritingDirection -1,HyphenationFactor 0,TighteningFactor 0.05, 标题级别 0"; }
NSSubTextStorage 可能是一个私有类,因为我找不到它的任何文档。它还保留所有格式。
I have an instance of NSData
containing attributed text (NSAttributedString
) originating from an NSTextView
. I want to convert the attributed string to a plain string (NSString
) without any formatting to do some text analysis (at the moment of conversion I do not have access to the originating NSTextView nor its NSTextStorage instance).
What would be the best way to do this?
EDIT:
Out of curiosity I examined the result of:
[[[self textView] textStorage] words]
which appeared to be a handy thing for doing some text analysis. The resulting array contains instances of NSSubTextStorage (example below of the word "Eastern"):
Eastern{
NSFont = "\"LucidaGrande 11.00 pt. P [] (0x7ffcaae08330) fobj=0x10a8472d0, spc=3.48\"";
NSParagraphStyle = "Alignment 0, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0,
FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0,
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 0, Blocks (null), Lists (null),
BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0.05,
HeaderLevel 0"; }
NSSubTextStorage is probably a private class as I could not find any documentation for it. It also retains all formatting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果我理解正确的话,你有一个
NSData
,比如说data
,其中包含一个编码的NSAttributedString
。要反转该过程:并获取没有属性的纯文本,然后执行以下操作:
If I understand you correctly you have an
NSData
, saydata
, containing an encodedNSAttributedString
. To reverse the process:and to get the plain text without attributes you then do:
更新 Swift 5:
Updating for Swift 5:
在 Swift 5 和 macOS 10.0+ 中,
NSAttributedString
有一个名为字符串
。string
具有以下声明:Apple 还对
string
进行了说明:以下 Playground 代码演示了如何使用
NSAttributedString
的string
属性来检索NSAttributedString
实例的字符串内容:With Swift 5 and macOS 10.0+,
NSAttributedString
has a property calledstring
.string
has the following declaration:Apple also states about
string
:The following Playground code shows how to use
NSAttributedString
'sstring
property in order to retrieve the string content of anNSAttributedString
instance:从 Swift 5.7(或更早版本)开始,新的 AttributedString 结构不再具有
字符串
属性。下面的代码可以工作,即使看起来很傻。As of Swift 5.7 (or maybe earlier), the new AttributedString struct no longer has a
string
property. The code below works, even looking silly.稍微扩展@Juguang的答案:
用法:
当前的Swift(截至2024年12月,Xcode 16.1,Swift 5.10)不会接受早期的答案,但这似乎工作正常,并且这个线程是搜索时首先出现的“Swift AttributedString 到纯文本”。
To expand slightly on @Juguang's answer:
Usage:
Current Swift (as of December 2024, Xcode 16.1, Swift 5.10) won't accept the earlier answers, but this seems to work fine, and this thread is what comes up first when searching for "Swift AttributedString to plaintext".