How to read .rtf File from URL in iPhone App

你离开我真会死。 提交于 2019-12-12 19:13:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!