2

您好,我尝试在 MKPolygonView 中绘制文本。我创建了 MKPolygonView 的子类并将其添加到我的 MKMapView。多边形正确显示,但我看不到文本。谁能帮我?

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

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

  CGRect overallCGRect = [self rectForMapRect:self.overlay.boundingMapRect];
  UIFont* font = [UIFont fontWithName:@"ArialRoundedMTBold" size:20.0f]; 

  NSString * t= @"Test";
  [[UIColor redColor] set];
  [t drawInRect:overallCGRect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
}
4

2 回答 2

0

我相当确定您需要在您的 drawMapRect 覆盖中使用 CoreGraphics 进行任何类型的绘图。下面的代码还没有编译,所以我不能保证它可以开箱即用,但是沿着这些线的东西可能会完成这项工作。

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

  // The base implementation does nothing so this isn't needed
  //[super drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context];

  NSString * t= @"Test" ;
  CGPoint point = [self pointForMapPoint:mapRect.origin];

  CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
  CGContextSelectFont (context, "Helvetica", 20.0f, kCGEncodingFontSpecific);
  CGContextShowTextAtPoint(context, point.x, point.y, [t UTF8String], [t length]);
}
于 2010-11-12T15:59:32.153 回答
0

我认为您可以通过将上下文推送到 UI 图形上下文堆栈,然后将其弹出来使用 UIKit 绘图,如下所示:

UIGraphicsPushContext(context);
[[UIColor redColor] set];
[t drawInRect:...];
etc, etc.
UIGraphicsPopContext();
于 2010-11-12T16:09:45.123 回答