如何在iPhone应用程序中从URL读取.rtf文件

如何在iPhone应用程序中从URL读取.rtf文件,iphone,ios,uitextview,rtf,readfile,Iphone,Ios,Uitextview,Rtf,Readfile,我的应用程序需要从URL读取.rtf文件。我将把它读为 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@.rtf",_URL]]; NSError* error; NSString *content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; 现在,当我在U

我的应用程序需要从URL读取.rtf文件。我将把它读为

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@.rtf",_URL]];
    NSError* error;
    NSString *content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];

现在,当我在UITextView中加载此文本时。它为我提供了带有{和/字符的标记以及原始文件文本。请指导我如何读取不包含任何括号和html数据的正确文本。

。rtf文件始终包含用于格式化文本颜色对齐和其他属性的标记

请在中打开该.rtf文件进行文本编辑,并将该文件的所有文本部分转换为简单文本

那么您现有的代码就可以工作了

否则,在UIWebview中打开它,而不是使用UItextView


希望对您有所帮助。

.rtf文件本身始终包含用于格式化文本颜色对齐和其他属性的标记

请在中打开该.rtf文件进行文本编辑,并将该文件的所有文本部分转换为简单文本

那么您现有的代码就可以工作了

否则,在UIWebview中打开它,而不是使用UItextView


希望它能对您有所帮助。

您可以在UITextView中使用RTF,方法是首先将其转换为NSAttributedString,然后将UITextView的attributedString属性的值设置为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];

可以在UITextView中使用RTF,方法是首先将其转换为NSAttributedString,然后将UITextView的attributedString属性的值设置为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];

NSURL*url=[NSURL URLWithString:@;NSURLRequest*req=[NSURLRequestWithURL:url];[webView loadRequest:req];它在webView中工作。感谢NSURL*url=[NSURL URLWithString:@;NSURLRequest*req=[NSURLRequestWithURL:url];[webView loadRequest:req];它在webView中工作。谢谢