2012-04-05 7 views
6

Ich möchte Text mit einer anderen Farbe in seiner Grenze (Umriss) anzeigen. Ich versuche, einen Text in MapOverlayView anzuzeigenWie könnte ich Text font umreißen?

[text drawAtPoint:CGPointMake(0,30) withFont:[UIFont fontWithName:@"Helvetica-Bold" size:(3 * MKRoadWidthAtZoomScale(zoomScale))] 

mit es funktioniert gut, außer ich Text müssen angezeigt Umrisse sein.

Antwort

8

Ja, Sie können umrissenen Text mit Hilfe von CGContextSetDrawingMode(CGContextRef, CGTextDrawingMode) anzeigen, obwohl Sie wahrscheinlich einige Zahlen und Farben anpassen müssen, damit es gut aussieht.

Es scheint logisch zu sein, kCGTextFillStroke zu verwenden, aber das kann dazu führen, dass der Strich die Füllung überwältigt. Wenn Sie streichen, dann füllen Sie, wie in dem Block unten, erhalten Sie einen sichtbaren Umriss hinter lesbarem Text.

CGContextRef context = UIGraphicsGetCurrentContext(); 

CGPoint point = CGPointMake(0,30); 
CGFloat fontSize = (3 * MKRoadWidthAtZoomScale(zoomScale)); 
UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:fontSize]; 

// Draw outlined text. 
CGContextSetTextDrawingMode(context, kCGTextStroke); 
// Make the thickness of the outline a function of the font size in use. 
CGContextSetLineWidth(context, fontSize/18); 
CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]); 
[text drawAtPoint:point withFont:font]; 

// Draw filled text. This will make sure it's clearly readable, while leaving some outline behind it. 
CGContextSetTextDrawingMode(context, kCGTextFill); 
CGContextSetFillColorWithColor(context, [[UIColor blueColor] CGColor]); 
[text drawAtPoint:point withFont:font]; 
+0

Vielen Dank ... es hat super funktioniert !! – user836026

+2

funktioniert nicht in meinem Fall, ich folgte den gleichen Schritten –

0

Die akzeptierte Antwort für mich nicht möglich arbeiten, weil drawAtPoint:withFont: veraltet. Ich konnte es mit dem folgenden Code arbeiten:

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGFloat fontSize = 18.0; 
CGPoint point = CGPointMake(0, 0); 

UIFont *font = [UIFont fontWithName:@"Arial-BoldMT" size:fontSize]; 
UIColor *outline = [UIColor whiteColor]; 
UIColor *fill = [UIColor blackColor]; 

NSDictionary *labelAttr = @{NSForegroundColorAttributeName:outline, NSFontAttributeName:font}; 

CGContextSetTextDrawingMode(context, kCGTextStroke); 
CGContextSetLineWidth(context, 2.0); 
[text drawAtPoint:point withAttributes:labelAttr]; 

CGContextSetTextDrawingMode(context, kCGTextFill); 
CGContextSetLineWidth(context, 2.0); 
labelAttr = @{NSForegroundColorAttributeName:fill, NSFontAttributeName:font}; 
[text drawAtPoint:point withAttributes:labelAttr];