2013-04-26 109 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

相關問題