将 NSAttributedString 转换为纯文本

发布于 2024-12-29 06:14:16 字数 977 浏览 1 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(5

风轻花落早 2025-01-05 06:14:16

如果我理解正确的话,你有一个 NSData,比如说 data,其中包含一个编码的 NSAttributedString。要反转该过程:

NSAttributedString *nas = [[NSAttributedString alloc] initWithData:data
                                                           options:nil
                                                documentAttributes:NULL
                                                             error:NULL];

并获取没有属性的纯文本,然后执行以下操作:

NSString *str = [nas string];

If I understand you correctly you have an NSData, say data, containing an encoded NSAttributedString. To reverse the process:

NSAttributedString *nas = [[NSAttributedString alloc] initWithData:data
                                                           options:nil
                                                documentAttributes:NULL
                                                             error:NULL];

and to get the plain text without attributes you then do:

NSString *str = [nas string];
暗喜 2025-01-05 06:14:16

更新 Swift 5:

attributedText.string

Updating for Swift 5:

attributedText.string
方圜几里 2025-01-05 06:14:16

在 Swift 5 和 macOS 10.0+ 中,NSAttributedString 有一个名为 字符串string 具有以下声明:

var string: String { get }

作为 NSString 对象的接收者的字符内容。

Apple 还对 string 进行了说明:

附件字符不会从此属性的值中删除。 [...]


以下 Playground 代码演示了如何使用 NSAttributedStringstring 属性来检索 NSAttributedString 实例的字符串内容:

import Cocoa

let string = "Some text"
let attributes = [NSAttributedString.Key.underlineStyle : NSUnderlineStyle.single]
let attributedString = NSAttributedString(string: string, attributes: attributes)

/* later */

let newString = attributedString.string
print(newString) // prints: "Some text"
print(type(of: newString)) // prints: String

With Swift 5 and macOS 10.0+, NSAttributedString has a property called string. string has the following declaration:

var string: String { get }

The character contents of the receiver as an NSString object.

Apple also states about string:

Attachment characters are not removed from the value of this property. [...]


The following Playground code shows how to use NSAttributedString's string property in order to retrieve the string content of an NSAttributedString instance:

import Cocoa

let string = "Some text"
let attributes = [NSAttributedString.Key.underlineStyle : NSUnderlineStyle.single]
let attributedString = NSAttributedString(string: string, attributes: attributes)

/* later */

let newString = attributedString.string
print(newString) // prints: "Some text"
print(type(of: newString)) // prints: String
勿忘心安 2025-01-05 06:14:16

从 Swift 5.7(或更早版本)开始,新的 AttributedString 结构不再具有字符串属性。下面的代码可以工作,即使看起来很傻。

part.characters.map { String($0) }.joined(separator: "")

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.

part.characters.map { String($0) }.joined(separator: "")
赠意 2025-01-05 06:14:16

稍微扩展@Juguang的答案:

extension AttributedString {
    func toString() -> String {
        return self.characters.map { String($0) }.joined(separator: "")
    }
}

用法:

print("Working value = \(workingAttribStrng.toString())")

当前的Swift(截至2024年12月,Xcode 16.1,Swift 5.10)不会接受早期的答案,但这似乎工作正常,并且这个线程是搜索时首先出现的“Swift AttributedString 到纯文本”。

To expand slightly on @Juguang's answer:

extension AttributedString {
    func toString() -> String {
        return self.characters.map { String($0) }.joined(separator: "")
    }
}

Usage:

print("Working value = \(workingAttribStrng.toString())")

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".

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