2011-10-19 10 views
10

Estoy intentando dibujar algunas superposiciones de círculo que contienen texto en MKMapView. He subclasificado el MKCircleView, en el que pongo lo siguiente (basado en this), pero el texto no aparece. Los círculos aparecen correctamente. (También probé la solución de la primera respuesta, el mismo resultado).Dibujar texto en superposición de círculo

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context { 
    [super drawMapRect:mapRect zoomScale:zoomScale inContext:context]; 
    NSString * t= @"XXXXX\nXXXX" ; 
    UIGraphicsPushContext(context); 
    CGContextSaveGState(context); 
    [[UIColor redColor] set]; 
    CGRect overallCGRect = [self rectForMapRect:[self.overlay boundingMapRect]]; 
    NSLog(@"MKC : %lf, %lf ----> %lf , %lf ", mapRect.origin.x ,mapRect.origin.y , overallCGRect.origin.x, overallCGRect.origin.y); 
    [t drawInRect:overallCGRect withFont:[UIFont fontWithName:@"Arial" size:10.0] lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter]; 
    CGContextRestoreGState(context); 
    UIGraphicsPopContext(); 
} 

Cuando la depuración, consigo valores como estos

MKC : 43253760.000000, 104071168.000000 ----> 1.776503 , 1.999245 
MKC : 43253760.000000, 104071168.000000 ----> -1.562442 , -2.043090 

¿Son normales? Qué me estoy perdiendo ?

Gracias.

+0

Combinando esto con la respuesta a continuación resuelto mi pregunta sobre cómo dibujar el texto. Sin embargo, mi texto se centra horizontalmente, pero verticalmente se dibuja en la parte superior de mi MKCircleView. ¿Está tuyo centrado vertical y horizontalmente? – Diziet

+0

Estaba calculando mal la ubicación del texto. Hohum. : P – Diziet

+2

Si alguien lo necesita, así es como centrado el texto en el círculo, teniendo en cuenta el ancho y alto del texto: 'Tamaño CGSize = [str sizeWithFont: [UIFont fontWithName: @" Arial "size: 9]]; CGPoint center = CGPointMake (circleRect.origin.x + circleRect.size.width /2,circleRect.origin.y + circleRect.size.height/2); CGPoint textstart = CGPointMake (center.x - size.width/2, center.y - size.height/2); ' – Templar

Respuesta

12

Creo que su código está funcionando y el problema es que el texto no se está escalando correctamente por lo que es invisible.

Escala el tamaño de fuente en base al zoomScale usando la función MKRoadWidthAtZoomScale:

[t drawInRect:overallCGRect withFont:[UIFont fontWithName:@"Arial" 
    size:(10.0 * MKRoadWidthAtZoomScale(zoomScale))] 
    lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter]; 

También asegúrese de usar un color de texto que es diferente del color del círculo subyacente.

Tenga en cuenta que el uso de drawInRect dará lugar a que el texto esté restringido dentro del círculo y pueda quedar truncado. Si desea mostrar siempre todo el texto, puede usar drawAtPoint en su lugar.

+0

¡Muchas gracias! Está funcionando ahora :) Olvidé tener en cuenta el nivel de zoom. Calculé el centro del círculo y el punto para comenzar de acuerdo con el tamaño del texto y con drawAtPoint se muestra perfectamente centrado. – Templar

2

Es muy probable que su texto se dibuje en un rectángulo que no es visible.

Lo primero que haría es intentar imprimir los valores como% f en vez de% lf, ya que esos valores parecen disparatados. También debe imprimir .size.width y .size.height para las dos rectas (mapRect y overallCGRect).

Si eso no lo lleva a una definición de rectángulo sensible, intente definir un CGRect como CGRectMake(0,0,100,20) y vea si el texto se dibuja.

También puede intentar simplemente dibujando un rectángulo lleno en el mismo overallCGRect en el que está dibujando su texto.

3

La combinación de las respuestas aquí y actualización de iOS7:

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context 
{ 
    [super drawMapRect:mapRect zoomScale:zoomScale inContext:context]; 

    UIGraphicsPushContext(context); 
    CGContextSaveGState(context); 
    [[UIColor blueColor] set]; 

    NSDictionary *fontAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:10.0f * MKRoadWidthAtZoomScale(zoomScale)]}; 
    CGSize size = [[[self overlay] title] sizeWithAttributes:fontAttributes]; 
    CGFloat height = ceilf(size.height); 
    CGFloat width = ceilf(size.width); 

    CGRect circleRect = [self rectForMapRect:[self.overlay boundingMapRect]]; 
    CGPoint center = CGPointMake(circleRect.origin.x + circleRect.size.width /2, circleRect.origin.y + circleRect.size.height /2); 
    CGPoint textstart = CGPointMake(center.x - width/2, center.y - height /2); 

    [[[self overlay] title] drawAtPoint:textstart withAttributes:fontAttributes]; 

    CGContextRestoreGState(context); 
    UIGraphicsPopContext(); 
} 
Cuestiones relacionadas