在 UITextView 中显示 txt 文件

发布于 2024-12-12 08:10:24 字数 1528 浏览 5 评论 0原文

我是 iPhone 开发新手,需要帮助。我正在开发一个包含 UITextView 的 iPhone 应用程序。我的文档中有一个 txt 文件列表。我试图使用 UITextView 来显示文本文件的内容,但我很难让它显示任何东西。我使用这些代码希望 UITextView 显示 txt 文件:

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
self.frlistName2 = [[manager contentsOfDirectoryAtPath:documentsDirectoryPath error:nil]pathsMatchingExtensions:[NSArray arrayWithObject:@"txt"]];
NSString *obj = @".txt";
frlisturls2 = [[NSMutableArray alloc] initWithObjects:nil];
NSString *temp;
NSMutableString *mp3Path = [[NSMutableArray alloc] init];
int j =0;
for(int i =0 ; i< [frlistName2 count]; i++)
{
    mp3Path  = [NSMutableString stringWithString:documentsDirectoryPath];
    [mp3Path appendString:@"/"];

    temp = [frlistName2 objectAtIndex:i];
    if([temp hasSuffix :obj])
    {
        [mp3Path appendString: temp];
        [frlisturls2 addObject: mp3Path];
        j++;
    }
    [mp3Path release];
}
NSLog(@"frlisturls2 is%@:",frlisturls2);
[super viewDidLoad];
fileName = [[NSString alloc]init];
fileName=[frlisturls2 objectAtIndex:iRow ];
NSLog(@"the fileName is %@",fileName);
stringSources = [NSString stringWithContentsOfFile: fileName encoding:NSUTF8StringEncoding error:nil];
frtextview=[[UITextView alloc]init];
[frtextview setText:stringSources];
NSLog(@"the file is :%@",stringSources);

文件名显示正确的 url,但 stringSource 没有单词。很抱歉这么长的帖子!希望对您有所帮助!

I am new to iPhone development,need help.I am developing an iPhone application which contains a UITextView.I have a list of txt files in the documents .I am trying to use a UITextView to display the contents of the text file, but I have trouble getting it to display anything at all. I use these codes want the UITextView to display the txt file :

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
self.frlistName2 = [[manager contentsOfDirectoryAtPath:documentsDirectoryPath error:nil]pathsMatchingExtensions:[NSArray arrayWithObject:@"txt"]];
NSString *obj = @".txt";
frlisturls2 = [[NSMutableArray alloc] initWithObjects:nil];
NSString *temp;
NSMutableString *mp3Path = [[NSMutableArray alloc] init];
int j =0;
for(int i =0 ; i< [frlistName2 count]; i++)
{
    mp3Path  = [NSMutableString stringWithString:documentsDirectoryPath];
    [mp3Path appendString:@"/"];

    temp = [frlistName2 objectAtIndex:i];
    if([temp hasSuffix :obj])
    {
        [mp3Path appendString: temp];
        [frlisturls2 addObject: mp3Path];
        j++;
    }
    [mp3Path release];
}
NSLog(@"frlisturls2 is%@:",frlisturls2);
[super viewDidLoad];
fileName = [[NSString alloc]init];
fileName=[frlisturls2 objectAtIndex:iRow ];
NSLog(@"the fileName is %@",fileName);
stringSources = [NSString stringWithContentsOfFile: fileName encoding:NSUTF8StringEncoding error:nil];
frtextview=[[UITextView alloc]init];
[frtextview setText:stringSources];
NSLog(@"the file is :%@",stringSources);

the filename display the right url,but the stringSource do not have words.Sorry for the long post!I hope for you help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦醒灬来后我 2024-12-19 08:10:24

这就是我将txt文件读取到txtView的方式,

但有时文件无法通过文本编辑器打开,

NSString *filePath = @"Users/macbookpro/Library/Application
 Support/iPhone Simulator/4.3.2/Applications/ ...
 /Documents/sample.txt"; 
NSString *txtContent = [[NSString alloc]
                                    initWithContentsOfFile:filePath
                                    encoding:NSUTF8StringEncoding
                                    error:&error];

将txtContent加载到textView.text

This is how I read the txt file to txtView

But sometimes the file can't open by text editor ,

NSString *filePath = @"Users/macbookpro/Library/Application
 Support/iPhone Simulator/4.3.2/Applications/ ...
 /Documents/sample.txt"; 
NSString *txtContent = [[NSString alloc]
                                    initWithContentsOfFile:filePath
                                    encoding:NSUTF8StringEncoding
                                    error:&error];

load txtContent to your textView.text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文