2015-06-22 63 views
0

我畫一個表視圖在一個PDF像this tutorial和它的作品當表是如此之大就像在教程中,但我需要一張小桌子,所以我已經改變了:填充表 - 目標-C

int xOrigin = 350; 
    int yOrigin = 200; 

    int rowHeight = 10; 
    int columnWidth = 35; 

    int numberOfRows = 6; 
    int numberOfColumns = 3; 

    [self drawTableAt:CGPointMake(xOrigin, yOrigin) withRowHeight:rowHeight andColumnWidth:columnWidth andRowCount:numberOfRows andColumnCount:numberOfColumns]; 

    [self drawTableDataAt:CGPointMake(xOrigin, yOrigin) withRowHeight:rowHeight andColumnWidth:columnWidth andRowCount:numberOfRows andColumnCount:numberOfColumns]; 

現在的表是空的:(有任何關於我如何能再次填充表的任何IDEEA?

回答

0

參考的例子,你在方法中使用

+(void)drawTableDataAt:(CGPoint)origin 
    withRowHeight:(int)rowHeight 
    andColumnWidth:(int)columnWidth 
     andRowCount:(int)numberOfRows 
    andColumnCount:(int)numberOfColumns 

填充= 10

你的尺寸變更爲20

0

這種方法是使用什麼樣的數據表中填充

+(void)drawTableDataAt:(CGPoint)origin 
    withRowHeight:(int)rowHeight 
    andColumnWidth:(int)columnWidth 
     andRowCount:(int)numberOfRows 
    andColumnCount:(int)numberOfColumns 
{ 
    int padding = 10; 

    NSArray* headers = [NSArray arrayWithObjects:@"Quantity", @"Description", @"Unit price", @"Total", nil]; 
    NSArray* invoiceInfo1 = [NSArray arrayWithObjects:@"1", @"Development", @"$1000", @"$1000", nil]; 
    NSArray* invoiceInfo2 = [NSArray arrayWithObjects:@"1", @"Development", @"$1000", @"$1000", nil]; 
    NSArray* invoiceInfo3 = [NSArray arrayWithObjects:@"1", @"Development", @"$1000", @"$1000", nil]; 
    NSArray* invoiceInfo4 = [NSArray arrayWithObjects:@"1", @"Development", @"$1000", @"$1000", nil]; 

    NSArray* allInfo = [NSArray arrayWithObjects:headers, invoiceInfo1, invoiceInfo2, invoiceInfo3, invoiceInfo4, nil]; 

    for(int i = 0; i < [allInfo count]; i++) 
    { 
     NSArray* infoToDraw = [allInfo objectAtIndex:i]; 

     for (int j = 0; j < numberOfColumns; j++) 
     { 

      int newOriginX = origin.x + (j*columnWidth); 
      int newOriginY = origin.y + ((i+1)*rowHeight); 

      CGRect frame = CGRectMake(newOriginX + padding, newOriginY + padding, columnWidth, rowHeight); 


      [self drawText:[infoToDraw objectAtIndex:j] inFrame:frame]; 
     } 

    } 

} 

這下面粘貼的方法用於表格單元格內繪製文本顯示

+(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect 
{ 

    CFStringRef stringRef = (__bridge CFStringRef)textToDraw; 
    // Prepare the text using a Core Text Framesetter 
    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL); 
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText); 


    CGMutablePathRef framePath = CGPathCreateMutable(); 
    CGPathAddRect(framePath, NULL, frameRect); 

    // Get the frame that will do the rendering. 
    CFRange currentRange = CFRangeMake(0, 0); 
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL); 
    CGPathRelease(framePath); 

    // Get the graphics context. 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    // Put the text matrix into a known state. This ensures 
    // that no old scaling factors are left in place. 
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); 


    // Core Text draws from the bottom-left corner up, so flip 
    // the current transform prior to drawing. 
    CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2); 
    CGContextScaleCTM(currentContext, 1.0, -1.0); 

    // Draw the frame. 
    CTFrameDraw(frameRef, currentContext); 

    CGContextScaleCTM(currentContext, 1.0, -1.0); 
    CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2); 


    CFRelease(frameRef); 
    CFRelease(stringRef); 
    CFRelease(framesetter); 
} 

如果您想設置小字號的小單元格內的文字

-(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect 
{ 
    int length=[textToDraw length]; 
    CFStringRef string = (__bridge CFStringRef) textToDraw; 
    CFMutableAttributedStringRef currentText = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); 

    CFAttributedStringReplaceString (currentText,CFRangeMake(0, 0), string); 
    CTFontRef font = CTFontCreateWithName((CFStringRef)@"Helvetica", 8.0f, nil); 
    CFAttributedStringSetAttribute(currentText,CFRangeMake(0, length),kCTFontAttributeName,font); 

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText); 

    CGMutablePathRef framePath = CGPathCreateMutable(); 
    CGPathAddRect(framePath, NULL, frameRect); 
    CFRange currentRange = CFRangeMake(0, 0); 
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL); 
    CGPathRelease(framePath); 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); 
    CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2); 
    CGContextScaleCTM(currentContext, 1.0, -1.0); 
    CTFrameDraw(frameRef, currentContext); 
    CGContextScaleCTM(currentContext, 1.0, -1.0); 
    CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2); 
    CFRelease(frameRef); 
    CFRelease(framesetter); 
}