2013-04-26 85 views

回答

3

执行此操作的一种方法是归档NSAttributedString值。直接键入答案大纲的示例代码:

NSTextView *myTextView; 
NSString *myFilename; 
... 
[NSKeyedarchiver archiveRootObject:myTextStorage.textStorage 
          toFile:myFilename]; 

读回:

myTextView.textStorage.attributedString = [NSKeyedUnarchiver unarchiveObjectWithFile:myFilename]; 

这就是创建和读取播放的文件就足够了。有创建NSData而不是文件的匹配方法,您可以将NSData转换为NSString或将其插入到NSDictionary中,并将其作为plist(XML)等序列化。

1

最好的办法是将文本保存为RFTD,并通过NSAttributedString将其加载到其他文本视图中。

// Load 
NSFileWrapper* filewrapper = [[NSFileWrapper alloc] initWithPath: path]; 
NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithFileWrapper: filewrapper]; 
NSAttributedString* origFile = [NSAttributedString attributedStringWithAttachment: attachment]; 

// Save 
NSData *data = [origFile RTFDFromRange: NSMakeRange(0, [origFile length]) documentAttributes: nil]; 
[[NSFileManager defaultManager] createFileAtPath: path contents: data attributes:nil]; 
+0

'NSTextAttachment'不可用在iOS上,所以“加载”部分将无法在那里工作。 – 2013-04-26 17:04:10

相关问题