RTFDファイルを表示させるサンプルを組んでみよう。
Xcodeで新規プロジェクトを作成した後、表示させたい RTFDファイルをテキストエディット.appで作成する。
知らなかったのだが、表組やリストなども作成できる。リンクの作り方も分かった。
Xcode内でもきちんと表示される。
InterfaceBuilderを立ち上げ、NSScrollViewをウィンドウへ貼付ける。この中の Custom View を NSTextViewに変える。
AppController クラスを追加し、先ほどの NSTextViewをアウトレットに接続しておく。
AppContorller.h
@interface AppController : NSObject {
IBOutlet NSTextView* _text_view;
}
@end
実装は sample.rtfd を NSTextViewで読み込むだけ。前回調べたメソッドを使っている。
AppContorller.m
@implementation AppController
- (void)awakeFromNib
{
NSString* path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"rtfd"];
NSLog(@"%@", path);
[_text_view readRTFDFromFile:path];
}
@end
実行してみよう。

出た。
面白い事にテキストの編集だけでなく、フォントの色やサイズなどの表示属性も変えることができる。

表組も編集できた。

なお表示のみで編集させたくない場合は - [NSTextView setEditable:] に NOを渡してやる。
[_text_view setEditable:NO];
NSTextView Class Reference - setEditable:
サンプル:RTFDsample.zip
- - - -
Cocoaで RTFDがかなり簡単に扱えることがわかった。文字装飾や画像のあるちょっとした文章をユーザに見せたい場合にこれは使える(しかもたった数行で)。