2012-07-31 9 views
12

tengo este trozo de código aquí que atrae a un bloque con una cadena de un caracter en él:Cómo cambiar el color de NSString en 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]]; 

Está funcionando muy bien, pero he estado haciendo un poco de diseño cambia, y ahora lo necesito para que la cadena dibujada sea blanca. Usar los métodos para configurar el color de relleno y el color de trazo no ha hecho nada. ¿Hay alguna otra forma de hacer esto?

Respuesta

10

Ha intentado:

CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), textColor); 

Por ejemplo:

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

Lo hice por blanco, no hizo nada ... – RaysonK

+0

@RaysonK actualizado –

+0

Bien, eso fue todo. Extraño, podría haber jurado que eso es lo que estaba haciendo ... ¡oh, problema resuelto, gracias! – RaysonK

7

Esto es lo que uso para las etiquetas de dibujo:

- (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

Ya probé CGContextSetFillColorWithColor, junto con CGContextSetStrokeColorWithColor – RaysonK

+1

@RaysonK lo que sugiero es usar mi solución alternativa, ya que creo que tuve los mismos problemas con drawAtPoint y es por eso que terminé usando ese –

+1

En realidad, esa llamada al método terminó trabajando. Gracias por la sugerencia, aunque – RaysonK

24

Ajuste el foregroundColor en los atributos y el uso de la dibujar con funciones de atributos

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

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

+3

Para mí, al menos, establecer el atributo 'NSForegroundColorAttributeName' trabajado correctamente, mientras que la solución en la respuesta aceptada no. – aroth

Cuestiones relacionadas