0

私の試したコードを以下に示します:-

#define FontFamily(fname,fsize) [UIFont fontWithName:fname size:fsize]
#define themeRed [UIColor colorWithRed:189.0f/255.0f green:0.0f/255.0f blue:12.0f/255.0f alpha:1]
#define helveticaBold @"HelveticaNeue-Bold"
#define helveticaReg @"HelveticaNeue"
#define M 16.0f
#define L 20.0f

書き込み用コードattributedString

 NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/MyUser"];
    NSFileManager *fm = [NSFileManager defaultManager];

    if (![fm fileExistsAtPath:docDir]) {
        [fm createDirectoryAtPath:docDir withIntermediateDirectories:NO attributes:nil error:nil];
    }

    NSString *filepath  = [docDir stringByAppendingPathComponent:@"WRP.rtf"];

    NSString *noteAsAttachment = [freehandTtl.text stringByAppendingString:[NSString stringWithFormat:@"\n%@",freehandTxt.text]];

    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:noteAsAttachment];

    [attributedStr addAttribute:NSFontAttributeName value:FontFamily(helveticaBold, L) range:NSMakeRange(0, freehandTtl.text.length)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:themeRed range:NSMakeRange(0, freehandTtl.text.length)];

    [attributedStr addAttribute:NSFontAttributeName value:FontFamily(helveticaReg, M) range:NSMakeRange(freehandTtl.text.length+1, freehandTxt.text.length)];

    NSLog(@"attr str is %@",attributedStr);

    [attributedStr fileWrapperFromRange:NSMakeRange(0, [attributedStr length])  documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType} error:nil];

    NSFileWrapper *fileWrapper = [[NSFileWrapper alloc] init];

    BOOL success = [fileWrapper writeToURL:[NSURL fileURLWithPath:filepath] options:NSFileWrapperWritingAtomic originalContentsURL:nil error:nil];

私の文字列値は以下のとおりです:--

attr str is My File Title {
    NSColor = "UIDeviceRGBColorSpace 0.741176 0 0.0470588 1";
    NSFont = "<UICTFont: 0xa140f00> font-family: \"Helvetica Neue\"; font-weight: bold; font-style: normal; font-size: 20.00pt";
}
{
}Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.{
    NSFont = "<UICTFont: 0x8ea1fc0> font-family: \"Helvetica Neue\"; font-weight: normal; font-style: normal; font-size: 16.00pt";
}

タイプBOOLの成功変数もyes、ファイルが正常に書き込まれたことを示します。しかし、自分のフォルダーの場所にある rtf ファイルを開こうとすると、以下のようなエラー メッセージが表示されます。

ここに画像の説明を入力

4

2 に答える 2

0

このコードは意味がありません:

[attributedStr fileWrapperFromRange:NSMakeRange(0, [attributedStr length])
                 documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType}
                              error:nil];

NSFileWrapper *fileWrapper = [[NSFileWrapper alloc] init];

BOOL success = [fileWrapper writeToURL:[NSURL fileURLWithPath:filepath]
                               options:NSFileWrapperWritingAtomic
                   originalContentsURL:nil
                                 error:nil];

ファイルラッパーの属性付き文字列を要求し、それが返すものを無視して、コンテンツなしで新しいものを作成しているため。これは成功する可能性が高くなります。

NSFileWrapper *fileWrapper = [attributedStr fileWrapperFromRange:NSMakeRange(0, [attributedStr length])
                 documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType}
                              error:nil];

BOOL success = [fileWrapper writeToURL:[NSURL fileURLWithPath:filepath]
                               options:NSFileWrapperWritingAtomic
                   originalContentsURL:nil
                                 error:nil];
于 2014-11-14T07:15:40.760 に答える
0

RTF を記述する別の方法。

BOOL success = [self writeAttributeString:attributedStr toPath:filepath];

-(BOOL)writeAttributeString:(NSMutableAttributedString*)attrStr toPath:(NSString*)path
{
    NSData *data = [attrStr dataFromRange:NSMakeRange(0, [attrStr length])
                       documentAttributes:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType,NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInteger:1]} error:nil];

    return [data writeToFile:path atomically:YES];
}

ここで、filePath には、rtf 拡張子を持つドキュメント ディレクトリへの rtf ファイル パスが含まれます。

于 2014-11-21T04:54:15.000 に答える