2013-04-26 3 views

답변

3

이 작업을 수행하는 한 가지 방법은 NSAttributedString 값을 보관하는 것입니다. 대답에 직접 입력 개요 샘플 코드 :

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

그것을 다시 읽어하려면 다음을 작성하고 파일을 다시 읽어하는 데 필요한 모든입니다

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

. NSData를 NSString으로 변환하거나 NSDictionary에 삽입하고 plist (XML) 등으로 serialize 할 수 있습니다.

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에서는 '로드'부분이 작동하지 않습니다. –

관련 문제