3

好的,我们在这里:

  • 我有一个NSTextView
  • 我得到它的NSMutableAttributedString内容
  • 我无法将其读/写到 plist

通过使用 Rob 的代码(在 NSAttributedString 中保存自定义属性),我取得了一些进展(我正在设法将数据写入磁盘),但我无法恢复它(=NSKeyedUnarchiver返回nil)。


编码:

// where MAS --> NSMutableAttributedString

NSData* stringData = [NSKeyedArchiver archivedDataWithRootObject:MAS];

解码:

NSMutableAttributedString* mas = (NSMutableAttributedString*)[NSKeyedUnarchiver unarchiveObjectWithData:dat];

有任何想法吗?任何可能的解决方法(即使不使用NSCoder,我怀疑它是否适用于 RTF...)都将受到欢迎!

4

1 回答 1

5

这就是它的解决方法。

NSAttributedString-> NSData

NSData* j = [(NSMutableAttributedString*)MAS RTFDFromRange:NSMakeRange(0,[[MAS string] length]) 
                                        documentAttributes:nil];

NSData->NSAttributedString

NSMutableAttributedString* mas = [[NSMutableAttributedString alloc] initWithRTFD:dat 
                                                              documentAttributes:nil];

就那么简单。

于 2012-09-10T17:56:37.203 回答