Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load rtf or text file into UITextView iphone sdk

Tags:

iphone

sdk

ios4

Hi i was wondering how should i load rtf or text file into UITextView i use several codes but did't work ,

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"txt"];
myTextView.text = filePath;

thank you .

like image 579
iOS.Lover Avatar asked Oct 05 '10 15:10

iOS.Lover


3 Answers

You may try with this:

NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
myTextView.text  = myText;
like image 105
vodkhang Avatar answered Oct 21 '22 18:10

vodkhang


myTextView.attributedText =
[   NSAttributedString.alloc
    initWithFileURL:[ NSBundle.mainBundle URLForResource:@"filename" withExtension:@"rtf"  ]
    options:nil
    documentAttributes:nil
    error:nullptr
];
like image 22
Satachito Avatar answered Oct 21 '22 17:10

Satachito


What you've done so far will get you the name of the file, you need to go one step further and actually read the contents of the file into an NSString, using something like:

NSError *err = nil;
NSString *fileContents = [NSString stringWithContentsOfFile:filePath 
                           encoding:NSUTF8StringEncoding
                           error:&err];
if (fileContents == nil) {
    NSLog("Error reading %@: %@", filePath, err);
} else {
    myTextView.text = fileContents;
}

That will work for plain text (assuming your file is in UTF8 encoding); you'll have to do something a lot fancier for RTF (UITextView doesn't know how to display RTF).

like image 23
David Gelhar Avatar answered Oct 21 '22 17:10

David Gelhar