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 .
You may try with this:
NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
myTextView.text = myText;
myTextView.attributedText =
[ NSAttributedString.alloc
initWithFileURL:[ NSBundle.mainBundle URLForResource:@"filename" withExtension:@"rtf" ]
options:nil
documentAttributes:nil
error:nullptr
];
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With