10

MKMapView にテキストを含む円オーバーレイを描画しようとしています。MKCircleView をサブクラス化し、そこに ( thisに基づいて) 以下を配置しましたが、テキストは表示されません。円は正しく表示されます。(最初の応答の解決策も試しましたが、同じ結果です)。

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
   [super drawMapRect:mapRect zoomScale:zoomScale inContext:context];
   NSString * t= @"XXXXX\nXXXX" ;
   UIGraphicsPushContext(context);
   CGContextSaveGState(context); 
   [[UIColor redColor] set];
   CGRect overallCGRect = [self rectForMapRect:[self.overlay boundingMapRect]];
   NSLog(@"MKC :  %lf, %lf ----> %lf , %lf ", mapRect.origin.x ,mapRect.origin.y , overallCGRect.origin.x, overallCGRect.origin.y);
   [t drawInRect:overallCGRect withFont:[UIFont fontWithName:@"Arial" size:10.0] lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter];
   CGContextRestoreGState(context);
   UIGraphicsPopContext();
}

デバッグすると、次のような値が得られます

MKC :  43253760.000000, 104071168.000000 ----> 1.776503 , 1.999245 
MKC :  43253760.000000, 104071168.000000 ----> -1.562442 , -2.043090

彼らは正常ですか?何が欠けていますか?

ありがとう。

4

3 に答える 3

12

あなたのコードは機能していると思いますが、問題はテキストが適切に拡大縮小されておらず、見えなくなっていることです。

関数のzoomScale使用に基づいてフォント サイズをスケーリングします。MKRoadWidthAtZoomScale

[t drawInRect:overallCGRect withFont:[UIFont fontWithName:@"Arial" 
    size:(10.0 * MKRoadWidthAtZoomScale(zoomScale))] 
    lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter];

また、下にある円の色とは異なるテキストの色を使用してください。

drawInRectを使用すると、テキストが円の内側に制限され、切り捨てられる場合があることに注意してください。常にすべてのテキストを表示したい場合は、drawAtPoint代わりに使用できます。

于 2011-10-21T03:10:28.973 に答える
4

ここでの回答を組み合わせて、IOS7 用に更新します。

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
    [super drawMapRect:mapRect zoomScale:zoomScale inContext:context];

    UIGraphicsPushContext(context);
    CGContextSaveGState(context);
    [[UIColor blueColor] set];

    NSDictionary *fontAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:10.0f * MKRoadWidthAtZoomScale(zoomScale)]};
    CGSize size = [[[self overlay] title] sizeWithAttributes:fontAttributes];
    CGFloat height = ceilf(size.height);
    CGFloat width  = ceilf(size.width);

    CGRect circleRect = [self rectForMapRect:[self.overlay boundingMapRect]];
    CGPoint center = CGPointMake(circleRect.origin.x + circleRect.size.width /2, circleRect.origin.y + circleRect.size.height /2);
    CGPoint textstart = CGPointMake(center.x - width/2, center.y - height /2 );

    [[[self overlay] title] drawAtPoint:textstart withAttributes:fontAttributes];

    CGContextRestoreGState(context);
    UIGraphicsPopContext();
}
于 2014-09-10T14:15:10.407 に答える
2

テキストが見えない長方形に描かれている可能性が最も高いです。

私が最初にすることは、値を %lf ではなく %f として出力することです。これらの値はおかしく見えるからです。また、2 つの四角形 ( and ) の.size.widthandも出力する必要があります。.size.heightmapRectoverallCGRect

それでも適切な長方形の定義につながらない場合は、自分で CGRect を定義してみてCGRectMake(0,0,100,20)、テキストが描画されるかどうかを確認してください。

テキストを描画しているのと同じように、塗りつぶされた長方形を単純に描画することもできoverallCGRectます。

于 2011-10-20T23:52:41.260 に答える