开发者

MKCircleview Drawings

开发者 https://www.devze.com 2023-01-07 16:45 出处:网络
How to draw a text in 开发者_StackOverflowMKCircleview? i have to print a text on the overlay of MKMapview. is it possible?Within this section of my overlay view subclass of MKOverlayView:

How to draw a text in 开发者_StackOverflowMKCircleview? i have to print a text on the overlay of MKMapview. is it possible?


Within this section of my overlay view subclass of MKOverlayView:

- (void)drawMapRect:(MKMapRect)mapRect
      zoomScale:(MKZoomScale)zoomScale
      inContext:(CGContextRef)context

You should do /something/ like this:

  CGContextSetTextMatrix(context, CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0));
  // Setup text rendering stuff.
  CGFloat fontHeight = rect.size.height/2.0f;
  CGContextSelectFont(context, "Thonburi", fontHeight, kCGEncodingMacRoman);
  CGContextSetRGBFillColor(context, 0, 0, 0, 1);
  CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
  // Set string.
  const char *text = [[NSString stringWithFormat:@"%d",clusters[i].count] UTF8String];
  int len = strlen(text);
  // Render text invisible.
  CGContextSetTextDrawingMode(context, kCGTextInvisible);
  CGContextShowTextAtPoint(context, 
                           0, 
                           0, 
                           text, 
                           strlen(text));
  // Get actual point it was renderered.
  CGPoint pt = CGContextGetTextPosition(context);
  // Set text to visible.
  CGContextSetTextDrawingMode(context, kCGTextFillStroke);
  // Actually render text.
  CGContextShowTextAtPoint(context,
                           rect.origin.x + (0.5f * rect.size.width) - (0.5f * pt.x), // Origin + half the width - half the width of text == center aligned text?
                           rect.origin.y + (1.25f * fontHeight),  // Hack.  1 1/4 font height (which is half rect height) == center vert. aligned text.
                           text, 
                           len);

This will render the text centered in the middle of the circle.

A few warnings about the above code... it's actually taken from a subclass of MKOverlayPathView within which I draw many many circles on the overlay view in different locations and thus needed a fairly dynamic method of drawing the text in the right place. Thus the code will probably not be a drop in solution for you and a bit more complicated than you actually need. However it should point you in the right direction.

Please bear in mind that having more than a couple of overlay views on a mapkit is a path to insanity with random crashes and stack traces as the layers consume more and more ram. Thus if you have more than a small handful of overlays I would recommend you also go down the MKOverlayPathView route.

I banged my head on the desk for almost a month before I discovered why it was crashing so much at random times.

My code is for clustering, placing text in the middle of each circle with the number of items in that cluster.

0

精彩评论

暂无评论...
验证码 换一张
取 消