问题
My app needs to read .rtf file from URL. I am going to read it as
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@.rtf",_URL]];
NSError* error;
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
Now when I load this text in UITextView. It gives me tags with { and / characters with the original file text. Please guide me that how can i read the right text which did not include any brackets and html data.
回答1:
.rtf file always contains Tags with itself for formating text color alignment and other properties..
please open that .rtf file in to text edit and convert all text part of that file to simple text.
then your existing code will work.
else instead of taking the UItextView open it in UIWebview.
hope it will be helpfull for you.
回答2:
You can use RTF in a UITextView by first transforming it into an NSAttributedString and then setting the value of the UITextView's attributedString property to the NSAttributedString.
NSAttributedString *stringWithRTF = [[NSAttributedString alloc] initWithFileURL:rtfDoc options:@{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType} documentAttributes:nil error:nil];
// Instantiate UITextView object
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20,20,self.view.frame.size.width,self.view.frame.size.height)];
textView.attributedText=stringWithRTF;
[self.view addSubview:textView];
来源:https://stackoverflow.com/questions/10686381/how-to-read-rtf-file-from-url-in-iphone-app