2012-07-31 8 views
12

Mam ten fragment kodu tutaj rysuje bloku z ciągiem jednej postaci na nim:Jak zmienić kolor NSString w drawAtPoint

CGContextDrawImage(context, CGRectMake([blok getLocation].x * xunit, [blok getLocation].y * yunit, 40, 40), [blok getImage].CGImage); 
[[blok getSymbol] drawAtPoint:CGPointMake([blok getLocation].x * xunit+15, [blok getLocation].y * yunit) withFont:[UIFont fontWithName:@"Helvetica" size:24]]; 

to działa dobrze, ale robiłem jakiś układ zmiany, a teraz potrzebuję go, aby ciąg narysowany był biały. Użycie metod ustawiania koloru wypełnienia i koloru obrysu niczego nie zmieniło. Czy jest jakiś inny sposób na zrobienie tego?

Odpowiedz

10

Czy próbowałeś:

CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), textColor); 

Na przykład:

CGContextDrawImage(context, CGRectMake([blok getLocation].x * xunit, [blok getLocation].y * yunit, 40, 40), [blok getImage].CGImage); 
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), textColor); 
[[blok getSymbol] drawAtPoint:CGPointMake([blok getLocation].x * xunit+15, [blok getLocation].y * yunit) withFont:[UIFont fontWithName:@"Helvetica" size:24]]; 
+1

Zrobiłem to dla białego, nic nie zrobiłem ... – RaysonK

+0

@RaysonK zaktualizowano –

+0

OK, to się udało. Dziwne, mógłbym przysiąc, że to właśnie robiłem ... no cóż, problem rozwiązany, dzięki! – RaysonK

7

To co mogę używać etykiet rysunek:

- (void)_drawLabel:(NSString *)label withFont:(UIFont *)font forWidth:(CGFloat)width 
      atPoint:(CGPoint)point withAlignment:(UITextAlignment)alignment color:(UIColor *)color 
{ 
    // obtain current context 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // save context state first 
    CGContextSaveGState(context); 

    // obtain size of drawn label 
    CGSize size = [label sizeWithFont:font 
          forWidth:width 
         lineBreakMode:UILineBreakModeClip]; 

    // determine correct rect for this label 
    CGRect rect = CGRectMake(point.x, point.y - (size.height/2), 
          width, size.height); 

    // set text color in context 
    CGContextSetFillColorWithColor(context, color.CGColor); 

    // draw text 
    [label drawInRect:rect 
      withFont:font 
     lineBreakMode:UILineBreakModeClip 
      alignment:alignment]; 

    // restore context state 
    CGContextRestoreGState(context); 
} 
+0

Próbowałem już CGContextSetFillColorWithColor, wraz z CGContextSetStrokeColorWithColor – RaysonK

+1

@RaysonK, co sugeruję, jest użycie mojego alternatywnego rozwiązania, ponieważ myślę, że miałem te same problemy z drawAtPoint i dlatego skończyło się na tym, że –

+1

W rzeczywistości, ta metoda zakończyła się pracujący. Dzięki za sugestię, ale: – RaysonK

24

Ustawić ForegroundColor w atrybutach i użyć rysuj z funkcjami Atrybuty

NSDictionary *attributes = [NSDictionary dictionaryWithObjects: 
          @[font, [UIColor whiteColor]] 
          forKeys: 
          @[NSFontAttributeName, NSForegroundColorAttributeName]]; 
[string drawInRect:frame withAttributes:attributes]; 
+3

Lub '[text drawInRect: CGRectIntegral (rect) withAttributes: @ {NSFontAttributeName: font, NSForegroundColorAttributeName: [UIColor whiteColor]}];' – Schultz9999

+3

Dla mnie przynajmniej ustawienie atrybutu 'NSForegroundColorAttributeName' działało poprawnie, podczas gdy rozwiązanie w zaakceptowanej odpowiedzi nie. – aroth