Search code examples
textloaduitextviewrtf

Loading Text from a .rtf


Hei guys, I'm trying to load a long text from a .rtf file and I want to show this text in a UITextView. I store all the .rtf files in a folder called "rtf" into the "Supporting Files" folder. This is my code.

- (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];
}

But It doesn't show me the text and I don't understand why... Thanks!


Solution

  • I just solved in this way:

    -(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;
            }  
        }  
    
    }
    

    I hope this will help some people in the future! :D