aryaxt / iOS-Rich-Text-Editor

A Rich Text Editor for iOS
Other
893 stars 158 forks source link

saving text #25

Closed scoobygoo closed 11 years ago

scoobygoo commented 11 years ago

Hi, is there a way to save text to a document?

aryaxt commented 11 years ago

Here is how it's done on iOS7. I might add this to the RichTextEdiotr on the next version.

Here you save attributedSting to an NSData, then you could write this NSData to file

    NSData *data = [self.rte.attributedText dataFromRange:NSMakeRange(0, self.rte.attributedText.length) documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType,
                                                                                                            NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
                                                error:nil];

Later you could read NSData from file and set it on rich text editor:

NSError *error;
NSAttributedString *str = [[NSAttributedString alloc] initWithData:data

                                                           options:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType,
                                                                     NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
                                                documentAttributes:nil error:&error];
// Check for any error here
self.rte.attributedText = str;