tags:

views:

88

answers:

2

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 .

+1  A: 

You may try with this:

NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
myTextView.text  = myText;
vodkhang
thank you doesn't work , would you please see the edited post ?
Mc.Lover
Wait, what is your textView, what is yourText, it seems that your variable name has problems. Can you print out the NSString after getting from `[NSString stringWithContentsOfFile...]`. I want to see the results there
vodkhang
myText is 'UITextView' and 'myTextView' is just a name, my file is a txt file with UTF8 Encoding .
Mc.Lover
did you do the NSLog() to see what's going on after you get the content, any content there? Did you try to put an NSError as a third variable like David said
vodkhang
yes , but the compiler shows me a warning: passing argument 1 of 'NSLog' from incompatible pointer type.
Mc.Lover
What? did you do something like NSLog(@"%@", error);
vodkhang
IAM SO SORRY I FORGOT TO CONNECT THE OUTLET WITH ON INTERFACE BUILDER SOOOOOO SORRY
Mc.Lover
+2  A: 

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).

David Gelhar
thank you doesn't work , would you please see the edited post ?
Mc.Lover
If it's not working, maybe you should check the error status (instead of ignoring it by passing `nil`).
David Gelhar