Draw simple Text in a MKPolygonView

試著忘記壹切 提交于 2019-12-11 01:46:03

问题


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

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/4165842/draw-simple-text-in-a-mkpolygonview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!