2
如何在MKCircleview中繪製文本?我必須在MKMapview的疊加層上打印文本。可能嗎?MKCircleview圖紙
如何在MKCircleview中繪製文本?我必須在MKMapview的疊加層上打印文本。可能嗎?MKCircleview圖紙
在MKOverlayView我的重疊視圖子類本節:
- (void)drawMapRect:(MKMapRect)mapRect
zoomScale:(MKZoomScale)zoomScale
inContext:(CGContextRef)context
你應該做/事/這樣的:
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);
,這會使得在中間居中文本這個圈子。
關於上述代碼的一些警告......它實際上來自MKOverlayPathView的一個子類,在該子類中,我在不同位置的疊加視圖上繪製了許多圓圈,因此需要一個相當動態的方法來繪製右側的文本地點。因此,代碼可能不會是您的解決方案,並且比您實際需要的複雜一點。但它應該指出你在正確的方向。
請記住,在mapkit上有多個覆蓋視圖是隨着圖層消耗越來越多的ram而導致隨機崩潰和堆棧痕跡的瘋狂路徑。因此,如果你有不止一小疊覆蓋,我建議你也沿着MKOverlayPathView路線走。
在我發現它爲什麼在隨機時間崩潰這麼多之前,我在桌子上敲了將近一個月。
我的代碼用於聚類,將文本置於每個圓圈的中間,並使用該羣集中的項目數。
+1對於**奇怪,爲什麼它在我的未解答的問題的頭版。** – 2013-12-05 08:55:20