2013-11-22 15 views
0

的問題是創建PDF兩個文本列如下: two independent text columns 爲了生成PDF我使用蘋果的指導「繪圖和打印指南適用於iOS」,兩列我創建標籤:如何在PDF中創建兩個文本列?

NSArray *stringList = 
    @[ 
     @[string1, string2], 
     @[@"", string4], 
     @[string5, string6], 
     @[string7, string8] 
    ]; 

    resultString = [[NSMutableAttributedString alloc] init]; 
    for (NSArray *row in stringList) { 
     int i = 0; 
     NSMutableAttributedString *subResult = [[NSMutableAttributedString alloc] init]; 
     NSMutableArray *tabs = @[].mutableCopy; 
     NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
     for (NSString *tempString in row) { 
      NSTextTab *tab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft location:200 options:nil]; 
      if ([tempString isKindOfClass:[NSAttributedString class]]) { 
       [subResult appendAttributedString:(NSAttributedString *)tempString]; 
       [subResult appendAttributedString:[[NSAttributedString alloc] initWithString:@"\t"]]; 
      } else { 
       [subResult appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\t", tempString]]]; 
      } 
      [tabs addObject:tab]; 
      i++; 
     } 
     [subResult appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]]; 
     [style setTabStops:tabs]; 
     [subResult addAttribute:NSParagraphStyleAttributeName 
          value:style 
          range:NSMakeRange(0, subResult.length)]; 
     [resultString appendAttributedString:subResult]; 
    } 

作爲輸出,我得到這個:

enter image description here 因此,我的字符串標有紅色箭頭,我希望看到完全在第二列,如圖片№1。

+0

真的沒有足夠的信息來幫助你 - 一個.plist只是一個XML文件,所以你需要詳細說明:你是否有問題.plist或創建PDF? – davbryn

+0

@davbryn問題在於創建PDF。我知道如何逐一繪製簡單的字符串,但我不明白如何在PDF中標記文本,如何製作縮進,段落等。 –

+0

你有沒有想過這個? – markturnip

回答

0

的想法是將文本分割成塊,單線條和用於每個部分

- (void)drawInRect:(CGRect)rect; 

void CTLineDraw(CTLineRef line, CGContextRef context); 

解決方案:

@import CoreText; 

<...> 

- (IBAction)createPDF:(id)sender { 
    PDFFileName = <yourString>; 
    PDFPath = <yourPath>; 
    [self generatePdfWithFilePath:PDFPath]; 
} 

- (void)generatePdfWithFilePath:(NSString *)thefilePath { 
    UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil); 
    BOOL done = NO; 
    do { 
     UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 712), nil); 
     [self drawText]; 
     done = YES; 
    } 
    while (!done); 
    UIGraphicsEndPDFContext(); 
} 

- (void)drawText { 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    //Here you can set up attributes for your text 
    NSDictionary *attributesForText = @{ 
             NSFontAttributeName:[UIFont fontWithName:@"Georgia" size:12] 
             }; 

    //Drawing text in line 
    NSMutableAttributedString *stringOneAttributed = [[NSMutableAttributedString alloc] initWithString:stringOne attributes:attributesForText]; 
    CTLineRef stringOneLine = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)(stringOneAttributed)); 
    CGContextSetTextPosition(currentContext, x, y); 
    CTLineDraw(stringOneLine, currentContext); 
    CFRelease(stringOneLine); 

    //Drawing block of text in rectangle 
    NSMutableAttributedString *stringTwoAttributed = [[NSMutableAttributedString alloc] initWithString:stringTwo attributes:attributesForText]; 
    [stringTwoAttributed drawInRect:CGRectMake(x, y, width, height)]; 
}