macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

initWithPath:documentAttributes:

RTF,RTFDファイルから属性付き文字列を作って初期化して返します
-(id)initWithPath:(NSString *)path:
             documentAttributes:(NSDictionary **)docuAttributes:

解説

RTF,RTFDファイルから属性付き文字列を作って初期化して返します。
文書属性を辞書で返します。
データを読むことができなければnilを返します。
【docuAttributes】ドキュメントの属性
●@"DocumentType" 文書タイプ
 NSPlainText プレーンテキスト
●@"CharacterEncoding" ファイルのエンコーディング NSStringEncoding
【NSStringEncoding】
● NSASCIIStringEncoding 7ビットASCIIエンコード
● NSNEXTSTEPStringEncoding NeXTSTEP拡張8ビットASCIIエンコード
● NSJapaneseEUCStringEncoding 日本語EUC
● NSUTF8StringEncoding 8ビットUnicode(UTF8)エンコード
● NSISOLatin1StringEncoding ISOラテン1エンコード
● NSISOLatin2StringEncoding ISOラテン2エンコード
● NSSymbolStringEncoding シンボルエンコード
● NSNonLossyASCIIStringEncoding 損失無し7ビットASCIIエンコード
● NSShiftJISStringEncoding シフトJIS
● NSUnicodeStringEncoding Unicodeエンコード
● NSWindowsCP1251StringEncoding アドビスタンダードCyrillic
● NSWindowsCP1252StringEncoding Winラテン1
● NSWindowsCP1253StringEncoding Greek
● NSWindowsCP1254StringEncoding Turkish
● NSWindowsCP1250StringEncoding Winラテン1
● NSISO2022JPStringEncoding ISO2022日本語エンコード(電子メールなど)
● NSMacOSRomanStringEncoding MacRoman
● NSProprietaryStringEncoding

  • (BOOL)readFromFile:(NSString*)fileName ofType:(NSString*)type

{
NSDictionary* attr;
//RTF,RTFDファイルから属性付き文字列を作って初期化して返します
NSAttributedString* attrStr = [[NSAttributedString alloc]
initWithPath:fileName documentAttributes:&attr];
[self setString:[attrStr string]];

// エンコーディング
aEncoding = [[attr objectForKey:@"CharacterEncoding"] intValue];

return YES;
}

返り値

( id )

文字列

引数

( NSString * )path

ファイルパス

( NSDictionary ** )docuAttributes

文書属性のポインタ

フレームワーク

ApplicationKit

クラス

NSAttributedStringAdditions

Instance Methods

使用可能

10.1

参照

例文

#import "MyObject.h"

@implementation MyObject

- (IBAction)myAction:(id)sender
{//開けるファイル拡張子の配列
    NSArray      *fileTypes    = [ NSArray arrayWithObject : @"rtf" ];
    //OpenPanelを作る
    NSOpenPanel  *opPanel       = [ NSOpenPanel openPanel ];
    
    NSAttributedString *aString;
    //OpenPanelの結果のボタン番号
    int		  opRet;
     
        //OpenPanelでファイル選択   
    opRet = [ opPanel runModalForDirectory : NSHomeDirectory() //どこのディレクトリを出すか
                                     file : @"Documents" //どのどのファイルを選択しておくか
                                    types : fileTypes ];//選べるファイルタイプ

    if ( opRet == NSOKButton ) {  // OPENPanelのボタンがOKなら
      aString = [[NSAttributedString allocWithZone:[self zone]] initWithPath:[opPanel filename] documentAttributes:NULL];
}
}
@end
//////////////////////
NSDictionary*		attr;
NSAttributedString*	attrStr = [[NSAttributedString alloc] 
                                    initWithPath:fileName documentAttributes:&attr];
aEncoding = [[attr objectForKey:@"CharacterEncoding"] intValue];