2010-11-12 41 views
2

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

回答

0

我相當確定您需要使用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]); 
} 
0

我想你就可以通過pushing the context使用的UIKit繪製到UI圖形上下文棧,然後彈出它之後,就像這樣:

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