【问题标题】:Draw simple Text in a MKPolygonView在 MKPolygonView 中绘制简单文本
【发布时间】:2010-11-12 14:54:48
【问题描述】:
您好,我尝试在 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];
}
【问题讨论】:
标签:
iphone
mkmapview
drawrect
【解决方案1】:
我相当确定您需要在您的 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]);
}
【解决方案2】:
我认为您将能够使用pushing the context 的 UIKit 绘图到 UI 图形上下文堆栈,然后将其弹出,如下所示:
UIGraphicsPushContext(context);
[[UIColor redColor] set];
[t drawInRect:...];
etc, etc.
UIGraphicsPopContext();