如何从我的应用程序包中读取文本文件并将其显示在UITextView中

时间:2021-02-25 09:17:57

I want to read a text file called "config.txt" from my application's bundle and display it in a UITextView. The code below is not working, can anyone help me?

我想从我的应用程序包中读取一个名为“config.txt”的文本文件,并将其显示在UITextView中。下面的代码不起作用,任何人都可以帮助我吗?

IF i want to display it on a View then what i should do as well.

如果我想在View上显示它,那么我应该做什么。

I have a IBOutlet to a UITextView, also please tell me how to change the text font and colour programmatically.

我有一个UITextView的IBOutlet,也请告诉我如何以编程方式更改文本字体和颜色。

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"config" ofType:@"txt"];  
if (filePath) {  
    NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding];
    if (myText) {  
        textView.text= myText;    
    }
}


[super viewDidLoad];

4 个解决方案

#1


1  

Check to make sure textView is not nil and that you setup the IBOutlet connection in Interface Builder.

检查以确保textView不是nil并且您在Interface Builder中设置了IBOutlet连接。

To set the font and color of the UITextView programmatically, use:

要以编程方式设置UITextView的字体和颜色,请使用:

textView.font = [UIFont fontWithName:@"Helvetica" size:18.0f];

and

textView.textColor = [UIColor blueColor];

See:

#2


0  

Is your file extension ".text" or ".txt"?

你的文件扩展名是“.text”还是“.txt”?

Also, why are you reading the file into an NSData object and then an NSString?

另外,为什么要将文件读入NSData对象然后是NSString?

#3


0  

see whether your config.txt is normal or messy code I mean you may check your txt file in the X-code surroundings

看看你的config.txt是正常的还是凌乱的代码我的意思是你可以在X-code环境中查看你的txt文件

#4


0  

The below code worked for me :

以下代码对我有用:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"config" ofType:@"txt"]; 

if (filePath) {  
    NSError *error;
    NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

    if(!error) {
        if (myText) {  //config file contains text
            textView.font = [UIFont fontWithName:@“Times New Roman” size:16.0f];
            textView.textColor = [UIColor blackColor];
            textView.text= myText;    
        }
    }
    else {
        //Handle error while reading file
    }
}
else {
    //File path does not exists. Handle error
}

#1


1  

Check to make sure textView is not nil and that you setup the IBOutlet connection in Interface Builder.

检查以确保textView不是nil并且您在Interface Builder中设置了IBOutlet连接。

To set the font and color of the UITextView programmatically, use:

要以编程方式设置UITextView的字体和颜色,请使用:

textView.font = [UIFont fontWithName:@"Helvetica" size:18.0f];

and

textView.textColor = [UIColor blueColor];

See:

#2


0  

Is your file extension ".text" or ".txt"?

你的文件扩展名是“.text”还是“.txt”?

Also, why are you reading the file into an NSData object and then an NSString?

另外,为什么要将文件读入NSData对象然后是NSString?

#3


0  

see whether your config.txt is normal or messy code I mean you may check your txt file in the X-code surroundings

看看你的config.txt是正常的还是凌乱的代码我的意思是你可以在X-code环境中查看你的txt文件

#4


0  

The below code worked for me :

以下代码对我有用:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"config" ofType:@"txt"]; 

if (filePath) {  
    NSError *error;
    NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

    if(!error) {
        if (myText) {  //config file contains text
            textView.font = [UIFont fontWithName:@“Times New Roman” size:16.0f];
            textView.textColor = [UIColor blackColor];
            textView.text= myText;    
        }
    }
    else {
        //Handle error while reading file
    }
}
else {
    //File path does not exists. Handle error
}