我的要求很简单 - 我有许多多边形覆盖视图要在我的地图视图中布置。在他们每个人中,我都想要一些文字。我需要根据用户在特定 MKPolygon 视图上的活动来更改文本和文本格式。
由于我的要求是多边形,我决定子类 MKPolygonRenderer 因为以下层次结构:
MKPolygonRenderer : MKOverlayPathRenderer : MKOverlayRenderer
我决定子类化MKPolygonRenderer
,因为我需要形状不规则的区域边界。在它们内部,我需要布置文本。这是我需要帮助的部分。
(好吧,基本上我需要在每个多边形内像 UILabel 一样灵活的东西,但在这里我正在努力进行最基本的操作)
这是我的MKPolygonRenderer
子类实现:
static UIFont * font = nil;
@implementation MyMapOverlayRenderer
-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
MKMapRect theMapRect = [self.overlay boundingMapRect];
if (!(MKMapRectIntersectsRect(mapRect, theMapRect)))
return;
NSString * t= @"Test" ;
CGRect theRect = [self rectForMapRect:theMapRect];
CGPoint point = CGPointMake((theRect.origin.x + theRect.size.width)/2, (theRect.origin.y + theRect.size.height)/2);
if (!font)
{
font = [UIFont fontWithName:@"Helvetica" size:20.0];
}
CGContextSaveGState(context);
CGContextSetTextDrawingMode(context, kCGTextFillStroke); // This is the default
[[UIColor blackColor] setFill];
[t drawAtPoint:point withAttributes:@{NSFontAttributeName:font}];
CGContextRestoreGState(context);
}
@end
不知何故,这里没有绘制任何文字。
我的其他观察:
point
包含非常大的值。我尝试用(15,15),(5,5)等替换,但没有成功。如果可能的话,我希望文本位于多边形的中心。- 我不知道我的
drawAtPoint
代码是否正确。 font
当不是静态的时候,我得到了同样的结果。- 从我的视图控制器中,我正在设置
fillColor
多边形覆盖视图的属性,否则它可以正常工作MKPolygonRenderer
,但不能与我的派生子类一起工作MyMapOverlayRenderer
。我看不到多边形的填充颜色。我在哪里可以在子类中设置它?