在MKPolygonView中绘制简单的文本
问题描述:
您好,我尝试在MKPolygonView中绘制文本。我做了一个MKPolygonView的子类,并将其添加到我的MKMapView。多边形正确显示,但我看不到文本。谁能帮我?在MKPolygonView中绘制简单的文本
-(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];
}
答
我相当确定您需要使用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]);
}
答
我想你就可以通过pushing the context使用的UIKit绘制到UI图形上下文栈,然后弹出它之后,就像这样:
UIGraphicsPushContext(context);
[[UIColor redColor] set];
[t drawInRect:...];
etc, etc.
UIGraphicsPopContext();