Draw simple Text in a MKPolygonView
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:(MKMa开发者_Python百科pRect)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];
}
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]);
}
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();
精彩评论