0

嘿伙计们,我正在尝试从 .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];
}

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

4

1 回答 1

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

于 2012-06-15T14:16:58.833 回答