2015-06-22 262 views
1

我有這種方法來繪製一個表螞蟻填充。我想要的是改變每一欄的一個詞的顏色,但我不知道我該怎麼做。有人可以幫我嗎?任何幫助將不勝感激。設置顏色CFAttributedStringRef

在我的應用程序

,用戶可以選擇從segmente控制某些屬性...我想他導出所選的內容在PDF,像一張桌子。所以在每一行字將被選中

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

    NSArray* headers = [NSArray arrayWithObjects:@"Grand", @"Taile ok", @"Petit", nil]; 
    NSArray* invoiceInfo1 = [NSArray arrayWithObjects:@"Extra", @"Bon", @"Ordi", nil]; 
    NSArray* invoiceInfo2 = [NSArray arrayWithObjects:@"Gras", @"Etat", @"Maigre", nil]; 
    NSArray* invoiceInfo3 = [NSArray arrayWithObjects:@"Cru", @"Propre", @"Sale", nil]; 
    NSArray* invoiceInfo4 = [NSArray arrayWithObjects:@"PLourd", @"PMoyen", @"PLeger", nil]; 
    NSArray* invoiceInfo5 = [NSArray arrayWithObjects:@"CSup", @"CEgal", @"CInf", nil]; 


    NSArray* allInfo = [NSArray arrayWithObjects:headers, invoiceInfo1, invoiceInfo2, invoiceInfo3, invoiceInfo4, invoiceInfo5,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]; 
     } 

    } 

} 

- (無效)的drawText:(的NSString *)textToDraw框內:(的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); 
} 

enter image description here enter image description here

+0

那麼每列的顏色或? –

+0

每行一個單元格的顏色。例如,我想讓下一個單元變成紅色:Grand,Bon,Gras,Sale,Pleger和CEgal。 –

+0

那麼基本上這個單詞會是隨機的,並且有一套顏色?這份名單會不會改變? –

回答

1

根據對這個問題的評論,你提到這些詞永遠不會改變。你可能會創建一大堆if/else語句來檢查每個選中的單詞對數組中的每個單詞。我已經把它作爲一個更有效的選擇,它應該有希望工作。它可能需要一些調整,甚至一個循環來遍歷您選擇的話,但這應該有希望把你在正確的方向:

//declare your textToDraw as a new NSString 
NSString *str = textToDraw; 
//Make an Array of the str by adding objects that are separated by whitespace 
NSArray *words = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
//create a BOOL to check if your selected word exists in the array 
BOOL wordExists = [words containsObject: @"%@", yourSelectedWord]; 

CTFramesetterRef framesetter = null; 

//if the word exists, make it red 
if(wordExists){ 

    NSUInteger indexOfTheString = [words indexOfObject: @"%@", yourSelectedWord]; 

    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL,str, NULL); 

    [currentText addAttribute:NSForegroundColorAttributeName 
     value:[UIColor redColor] 
     range:NSMakeRange(indexOfTheString, yourSelectedWord.length)]; 

    framesetter = CTFramesetterCreateWithAttributedString(currentText); 

} 

這將匹配你的陣列,突出對正確的字找到您所選擇的字它是紅色的。

+0

這應該在哪裏實施?對不起,但這是第一次當我做這樣的事情時:( –

+0

在你的drawText方法中,我以前沒有嘗試過,所以它可能需要一些玩弄,但理論上它應該可以工作,我想你可能已經完成了你的佈局更難處理,但我希望這可以工作。 –

+0

和標籤在哪裏?因爲我不使用標籤.... –