从.rtf加载文本

时间:2012-06-15 13:57:09

标签: text load uitextview rtf

嘿伙计们, 我正在尝试从.rtf文件加载长文本,我想在UITextView中显示此文本。 我将所有.rtf文件存储在名为“rtf”的文件夹中的“Supporting Files”文件夹中。 这是我的代码。

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;

        // Update the view.
        [self configureView];
    }

}

- (void)configureView
{
    // Update the user interface for the detail item.
    if (self.detailItem) 
    {
        self.textView.text = [self setTextForTextView:[self.detailItem description]];
    }

}

-(NSString *)setTextForTextView:(NSString *)description
{
    NSString *path = [NSString stringWithFormat:@"rtf/%@.rtf" ,description];
    NSLog(@"%@" ,path);
    NSString *myText = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    return myText;

}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.title = @"Text";
    // Do any additional setup after loading the view, typically from a nib.
    [self configureView];
}

但它没有向我显示文字,我不明白为什么...... 谢谢!

1 个答案:

答案 0 :(得分:0)

我刚刚用这种方式解决了:

-(NSString *)setTextForTextView:(NSString *)description
{

    NSString *filePath = [[NSBundle mainBundle] pathForResource:description ofType:@"txt"];  
    if (filePath) 
    {  
        NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];  

        if (myText) 
        {  
            return myText;
        }  
    }  

}

我希望这将有助于将来的某些人! :d