将rtf或文本文件加载到UITextView iphone sdk中

时间:2022-02-09 10:44:57

Hi i was wondering how should i load rtf or text file into UITextView i use several codes but did't work ,

嗨我想知道我应该如何将rtf或文本文件加载到UITextView中我使用了几个代码但是没有用,

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"txt"];
myTextView.text = filePath;

thank you .

谢谢 。

4 个解决方案

#1


14  

You may try with this:

你可以尝试这个:

NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
myTextView.text  = myText;

#2


9  

myTextView.attributedText =
[   NSAttributedString.alloc
    initWithFileURL:[ NSBundle.mainBundle URLForResource:@"filename" withExtension:@"rtf"  ]
    options:nil
    documentAttributes:nil
    error:nullptr
];

#3


8  

What you've done so far will get you the name of the file, you need to go one step further and actually read the contents of the file into an NSString, using something like:

到目前为止你所做的将获得文件的名称,你需要更进一步,实际上将文件的内容读入NSString,使用类似的东西:

NSError *err = nil;
NSString *fileContents = [NSString stringWithContentsOfFile:filePath 
                           encoding:NSUTF8StringEncoding
                           error:&err];
if (fileContents == nil) {
    NSLog("Error reading %@: %@", filePath, err);
} else {
    myTextView.text = fileContents;
}

That will work for plain text (assuming your file is in UTF8 encoding); you'll have to do something a lot fancier for RTF (UITextView doesn't know how to display RTF).

这适用于纯文本(假设您的文件采用UTF8编码);你必须为RTF做更多的事情(UITextView不知道如何显示RTF)。

#4


0  

  1. Drag your TextView control (I had to click 3 times until it said NSTextView) to your AppDelegate.m file underneath the first @interface line. Mine was @interface AppDelegate (). The reason for the three clicks is because by default when you drag a TextView control on a window, it creates 3 controls and it's only the inner, inner one that is set to NSTextView. The other controls deal with scrolling and screen clipping. Then, choose to create an outlet and name it something like txtRich or whatever you want. This created this entry for me in my case:
  2. 将TextView控件(我必须单击3次,直到它说NSTextView)拖到第一个@interface行下面的AppDelegate.m文件中。我的是@interface AppDelegate()。三次单击的原因是因为默认情况下,当您在窗口上拖动TextView控件时,它会创建3个控件,并且它只是设置为NSTextView的内部,内部控件。其他控件处理滚动和屏幕剪辑。然后,选择创建一个插座,并将其命名为txtRich或任何你想要的东西。在我的情况下,这为我创建了这个条目:

@property (unsafe_unretained) IBOutlet NSTextView *txtRich;
  1. Find a class method where you want to load the RTFD. I did mine in the AppDelegate.m under applicationDidFinishLaunching. Inside that, paste something like this:
  2. 找到要加载RTFD的类方法。我在applicationDidFinishLaunching下的AppDelegate.m中做了我的。在里面,粘贴这样的东西:

NSBundle *myBundle = [NSBundle mainBundle];
NSString *sFile= [myBundle pathForResource:@"myrichfile" ofType:@"rtfd"];
[self.txtRich readRTFDFromFile:sFile];

You may be wondering where this mainBundle comes from, and if you have to declare it somewhere. The answer is no. It's freaking magic, created by default, just like the NSNotificationCenter defaultCenter variable. It refers to your own application bundle.

你可能想知道这个mainBundle来自哪里,如果你必须在某个地方声明它。答案是不。默认创建它就像NSNotificationCenter defaultCenter变量一样神奇。它指的是您自己的应用程序包。

  1. Now, use TextEdit to create a file myrichfile.rtfd. Save it in your project folder. Drag it to your project window under the Supporting Files. When you get prompted, go with the defaults. This bundles it with your project in a Resources folder.
  2. 现在,使用TextEdit创建文件myrichfile.rtfd。将其保存在项目文件夹中。将其拖动到支持文件下的项目窗口。出现提示时,请使用默认值。这将它与您在Resources文件夹中的项目捆绑在一起。

#1


14  

You may try with this:

你可以尝试这个:

NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
myTextView.text  = myText;

#2


9  

myTextView.attributedText =
[   NSAttributedString.alloc
    initWithFileURL:[ NSBundle.mainBundle URLForResource:@"filename" withExtension:@"rtf"  ]
    options:nil
    documentAttributes:nil
    error:nullptr
];

#3


8  

What you've done so far will get you the name of the file, you need to go one step further and actually read the contents of the file into an NSString, using something like:

到目前为止你所做的将获得文件的名称,你需要更进一步,实际上将文件的内容读入NSString,使用类似的东西:

NSError *err = nil;
NSString *fileContents = [NSString stringWithContentsOfFile:filePath 
                           encoding:NSUTF8StringEncoding
                           error:&err];
if (fileContents == nil) {
    NSLog("Error reading %@: %@", filePath, err);
} else {
    myTextView.text = fileContents;
}

That will work for plain text (assuming your file is in UTF8 encoding); you'll have to do something a lot fancier for RTF (UITextView doesn't know how to display RTF).

这适用于纯文本(假设您的文件采用UTF8编码);你必须为RTF做更多的事情(UITextView不知道如何显示RTF)。

#4


0  

  1. Drag your TextView control (I had to click 3 times until it said NSTextView) to your AppDelegate.m file underneath the first @interface line. Mine was @interface AppDelegate (). The reason for the three clicks is because by default when you drag a TextView control on a window, it creates 3 controls and it's only the inner, inner one that is set to NSTextView. The other controls deal with scrolling and screen clipping. Then, choose to create an outlet and name it something like txtRich or whatever you want. This created this entry for me in my case:
  2. 将TextView控件(我必须单击3次,直到它说NSTextView)拖到第一个@interface行下面的AppDelegate.m文件中。我的是@interface AppDelegate()。三次单击的原因是因为默认情况下,当您在窗口上拖动TextView控件时,它会创建3个控件,并且它只是设置为NSTextView的内部,内部控件。其他控件处理滚动和屏幕剪辑。然后,选择创建一个插座,并将其命名为txtRich或任何你想要的东西。在我的情况下,这为我创建了这个条目:

@property (unsafe_unretained) IBOutlet NSTextView *txtRich;
  1. Find a class method where you want to load the RTFD. I did mine in the AppDelegate.m under applicationDidFinishLaunching. Inside that, paste something like this:
  2. 找到要加载RTFD的类方法。我在applicationDidFinishLaunching下的AppDelegate.m中做了我的。在里面,粘贴这样的东西:

NSBundle *myBundle = [NSBundle mainBundle];
NSString *sFile= [myBundle pathForResource:@"myrichfile" ofType:@"rtfd"];
[self.txtRich readRTFDFromFile:sFile];

You may be wondering where this mainBundle comes from, and if you have to declare it somewhere. The answer is no. It's freaking magic, created by default, just like the NSNotificationCenter defaultCenter variable. It refers to your own application bundle.

你可能想知道这个mainBundle来自哪里,如果你必须在某个地方声明它。答案是不。默认创建它就像NSNotificationCenter defaultCenter变量一样神奇。它指的是您自己的应用程序包。

  1. Now, use TextEdit to create a file myrichfile.rtfd. Save it in your project folder. Drag it to your project window under the Supporting Files. When you get prompted, go with the defaults. This bundles it with your project in a Resources folder.
  2. 现在,使用TextEdit创建文件myrichfile.rtfd。将其保存在项目文件夹中。将其拖动到支持文件下的项目窗口。出现提示时,请使用默认值。这将它与您在Resources文件夹中的项目捆绑在一起。