2011-11-04 2 views
2

MKAnnotation 서브 클래스에서 검색 할 수있는 클러스터 핀 수와 함께 맵 핀 클러스터를 나타내는 MKAnnotation 서브 클래스가 있습니다. 이러한 주석의 경우 클러스터의 핀 수를 나타내는 검은 색의 굵은 숫자로 회색 원을 표시하고 싶습니다. reuseIdentifier과의 drawRect : 나는 initWithAnnotation 구현하는 MKAnnotationView 서브 클래스했습니다 방법을, 이것은의 drawRect 내 구현 : 방법 :동적으로 그려지는 핀으로 MKAnnotationView 서브 클래스 얻기

- (void)drawRect:(CGRect)rect 
{ 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBFillColor(context, 0.7f, 0.7f, 0.7f, 0.5f); 
    CGContextFillEllipseInRect(context, rect); 

    UIFont *font = [UIFont boldSystemFontOfSize: [UIFont smallSystemFontSize]]; 
    NSString *label = [NSString stringWithFormat:@"%i", [(LocationGroupAnnotation *)self.annotation locationCount]]; 
    CGPoint labelLocation; 

    if ([label length] == 1) 
    { 
     labelLocation = CGPointMake(rect.size.width/2.0f, (rect.size.height/2.0f) - (font.capHeight/2.0f)); 
    } else if ([label length] == 2) { 
     labelLocation = CGPointMake(rect.size.width/2.0f, (rect.size.height/2.0f) - (font.capHeight/2.0f)); 
    } else { 
     labelLocation = CGPointMake(rect.size.width/2.0f, (rect.size.height/2.0f) - (font.capHeight/2.0f)); 
    } 


    [label drawAtPoint:labelLocation withFont:font]; 

    NSLog(@"Drawn label at (%f,%f)", labelLocation.x, labelLocation.y); 
} 

는 문 경우, 내가 갈거야에 labelLocation에 대한 값을 무시 각 글자가 얼마나 많은 공간을 차지했는지에 따라 숫자가 중앙에 오도록 조정하십시오. 지금 내가 얻고있는 것은 반투명의 회색 원이지만 텍스트는 없다. 나는 텍스트가 잘못된 위치에 그려지고 있다고 가정합니다. 또한 텍스트를 텍스트 뒤에 표시하지 않고 서클 앞에 표시하도록 지정하려면 어떻게해야합니까? 그것은 위의 CGContextSetRGBFillColor 호출에서 설정된 색상 사용 달리에

답변

4

오른쪽 drawAtPoint를 호출하기 전에, 텍스트를 원하는 색상을 설정

[[UIColor blackColor] set]; //or some color that contrasts with background 
[label drawAtPoint:labelLocation withFont:font]; 

당신이 원 후 텍스트를 그릴 경우를, 그 위에 나타납니다 원과 그 반대입니다.

관련 문제