2014-11-14 9 views
0

내 노력 코드가 아래와 같습니다 : - : 형 BOOL의rtf 파일에 NSMutableAttributedString을 쓸 수 없습니다?

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"; 
} 

과 성공 변수 - 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]; 

을 작성하기위한

#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 

코드 내 문자열 값은 아래와 같습니다 또한 예, 성공적으로 작성된 파일을 보여줍니다. 당신이 따라

[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]; 

:이 코드는 어떤 의미가 없습니다

enter image description here

+0

mac 파일에서 읽을 수없는 파일 때문에 쓰기 문제가 발생했습니다 – Warewolf

+0

명령 줄에서'cat file.rtf'를 실행하면 어떻게됩니까? 더 나은 오류가 있습니까? – trojanfoe

+0

아무 일도 없었습니다. 오류가 발생하지 않고 명령이 실행되었습니다. – Warewolf

답변

0

- : 내 폴더 위치에서 RTF 파일을 열려고하면 그러나 아래에 표시되는 오류 메시지가 표시 속성이있는 문자열에 파일 래퍼를 요청한 다음 반환되는 내용을 무시하고 내용이없는 새 항목을 만듭니다. 성공 가능성이 더 높습니다.

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]; 
+0

감사합니다. 이것은 바보 같은 실수였습니다. 처음 NSFileWrapper를 사용했지만이 코드는 TXT.rtf라는 파일 이름의 폴더를 작성하여 파일을 작성합니다. – Warewolf

+0

WRP-> TXT.rtf here WRP는 자동으로 폴더가 생성됩니다 – Warewolf

+0

@GhostRider 추가 문제가있는 경우 새로운 질문을 시작하십시오. – trojanfoe

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 파일 경로가 있습니다.