在 MKPolygonView 中绘制简单文本

发布于 2024-10-01 14:43:22 字数 663 浏览 1 评论 0原文

您好,我尝试在 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];
}

Hello I try to draw text in a MKPolygonView. I made a subclass of MKPolygonView and added it to my MKMapView. The Polygon shows up correctly, but I can't see the Text. Can anyone help me?

-(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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薔薇婲 2024-10-08 14:43:22

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

-(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]);
}

I'm fairly certain that you need to use CoreGraphics for any kind of drawing in your drawMapRect override. The code below has not been compiled so I can't guarantee that it will work out of the box, but something along these lines will probably do the job.

-(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]);
}
姐不稀罕 2024-10-08 14:43:22

我认为您将能够通过 将上下文推送到 UI 图形上下文堆栈,然后将其弹出,如下所示:

UIGraphicsPushContext(context);
[[UIColor redColor] set];
[t drawInRect:...];
etc, etc.
UIGraphicsPopContext();

I think you'll be able to use the UIKit drawing by pushing the context to the UI graphics context stack, then popping it afterwards, like so:

UIGraphicsPushContext(context);
[[UIColor redColor] set];
[t drawInRect:...];
etc, etc.
UIGraphicsPopContext();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文