【问题标题】:Convert NSAttributedString to string and back将 NSAttributedString 转换为字符串并返回
【发布时间】:2013-05-03 17:08:10
【问题描述】:

我有 NSString,我需要创建 NSAttributedString。

NSString 类似于:

bvcx b vcxbcvx bcxvbcxv bvx xbc bcvx bxcv bcxv bcxv bcxv bcvx bcvx bcxvbcvx bvc bcvx bxcv{
NSFont = "\"LucidaGrande 24.00 pt. P [] (0x108768a80) fobj=0x108788880, spc=7.59\"";
NSParagraphStyle = "Alignment 4, 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";
}

它是 UTF-8 格式的 NSAttributedString。有什么办法可以做到吗?

【问题讨论】:

  • 您在寻找NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:@"foo"];吗?
  • 不,我需要格式化的 nsattributestring。谢谢感兴趣。
  • 您的NSString 内容从何而来?
  • NSString 不是 UTF8。您的字符串似乎是某种文字处理器程序的输出(甚至可能是 NSAttributedString 的 description 输出——我从未见过),并且您需要了解该格式来处理它(如果还没有属性字符串)。 Objective-C 中没有“自动”机制来执行该翻译。
  • 或者,或者,执行NSLog(@"The class is %s", object_getClassName(myFunnyString));,然后查看打印的类名。

标签: objective-c cocoa nsstring nsattributedstring


【解决方案1】:

您说您从现有的NSAttributedString 创建了输入字符串,如下所示:

[NSString stringWithFormat:@"%@", nsattributedstring]

%@ 格式说明符将description 消息发送到nsattributedstring 对象。 description 方法并非旨在生成可以轻松转换回NSAttributedString 对象的字符串。它旨在帮助程序员调试他们的代码。

将对象转换为字符串或字节数组以便以后可以转换回对象的过程称为序列化。使用%@description 方法通常不是执行序列化的好方法。如果你真的想反序列化 description 方法创建的字符串,你必须编写自己的解析器。据我所知,没有 API。

相反,Cocoa 提供了一个旨在序列化和反序列化对象的系统。可以使用该系统序列化的对象符合NSCoding 协议。 NSAttributedString 对象符合 NSCoding。因此,请尝试以这种方式序列化您的原始属性字符串:

NSMutableData *data = [NSKeyedArchiver archivedDataWithRootObject:nsattributedstring];

data(非人类可读的二进制文件,不是UTF-8)保存在您需要的任何位置。当您需要重新创建属性字符串时,请执行以下操作:

NSAttributedString *fancyText = [NSKeyedUnarchiver unarchiveObjectWithData:data];

如果您正在为 OS X 编程不是 iOS),您还有其他选择。您可以使用RTFFromRange:documentAttributes: method(省略附件)或RTFDFromRange:documentAttributes: method(包括附件)将属性化字符串转换为人类可读的RTF(富文本格式)。然后您可以使用initWithRTF:documentAttributes:initWithRTFD:documentAttributes: 将RTF 数据转换回属性字符串。这些方法在 iOS 上不可用。

如果您正在为 iOS 7.0 或更高版本编程,您可以使用 -dataFromRange:documentAttributes:error:fileWrapperFromRange:documentAttributes:error: 将属性字符串转换为 RTF/RTFD。您需要在文档属性中将NSDocumentTypeDocumentAttribute 设置为NSRTFTextDocumentTypeNSRTFDTextDocumentType。使用initWithData:options:documentAttributes:error:initWithFileURL:options:documentAttributes:error: 转换回NSAttributedString。这些方法是NSAttributedString UIKit Additions 的一部分。

【讨论】:

  • 谢谢回复。我知道 NSCoding,但我需要获取 NSAttributedString 的 UTF-8,因为我正在将字符串写入 UTF-8 xml 文件。有什么办法可以做到吗?
  • @MichalJuriJurník:没有“NSAttributedString 的 UTF-8”之类的东西。 NSAttributedString 是一个字符串加上属性;您可以将任何字符串编码为 UTF-8 数据,但您仍然必须分别序列化属性,然后对它们进行反序列化/解码并在另一端将它们重新组合在一起。使用 RTF 方法;它们就是为此而设计的。
  • 当我尝试时,我得到了这个错误 - 如何解决它 - [NSConcreteMutableAttributedString _encodingCantBeStoredInEightBitCFString]:无法识别的选择器发送到实例 0xf078af0 *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'- [NSConcreteMutableAttributedString _encodingCantBeStoredInEightBitCFString]:无法识别的选择器发送到实例 0xf078af0'
  • @Dinesh 您需要将此作为新问题发布。包括您的代码和您尝试序列化的字符串的详细信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多