我正在尝试在uiwebview中显示rtf文件的内容。我这样做是将rtf的内容转换为nsmutablestring并将其加载到uiwebview中。
问题是rtf的内容,rtf标签和格式也在nsmutablestring中传递。所以我将整个内容与uiwebview中的rtf格式一起显示。
如何从nsmutable字符串中删除rtf格式和标签?????请帮助我。我在网上搜索了很多,没有令人满意的结果。
代码如下:
UIWebView *webView = [[UIWebView alloc]init];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Home" ofType:@"rtf"];
NSMutableString *rtfString = [[NSMutableString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
NSMutableString *htmlString = [NSMutableString stringWithFormat:@"<font face=\"verdana\" size=\"2\" color=\"green\">%@</font>",rtfString];
[webView loadHTMLString:htmlString baseURL:nil];
这是rtf文件的输出,显示在uiwebview
中{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf290
{\fonttbl\f0\fnil\fcharset0 LucidaGrande;}
{\colortbl;\red255\green255\blue255;}
\deftab560
\pard\tx560\pardeftab560\ql\qnatural\pardirnatural
\f0\fs22 \cf0 \CocoaLigature0 This is Sample Text. Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.\
\
\
Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.\
\
\
This is sample text.\
\
\
This is sample TExt.}
段落之间的所有“\”都是空格,表示段落结尾或段落开头。整个结果显示在uiwebview中。那我怎么格式化呢是否有任何类或框架,我应该使用???? Plz帮助我。
答案 0 :(得分:1)
我还没想出是否/如何添加文字格式,但如果您要查看rtf,则应使用loadRequest
代替loadHTMLString
。
UIWebView *webView = [[UIWebView alloc]init];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Home" ofType:@"rtf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];