2012-04-05 12 views
6

Me gustaría mostrar texto con otro color en su borde (contorno). Estoy tratando de mostrar un texto en MapOverlayView usando¿Cómo podría delinear la fuente de texto?

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

funciona bien, excepto que necesito texto para ser mostradas contornos.

Respuesta

8

Sí, puede mostrar el texto contorneado con la ayuda de CGContextSetDrawingMode(CGContextRef, CGTextDrawingMode), aunque es probable que deba ajustar algunos números y colores para que se vea bien.

Parece lógico usar kCGTextFillStroke, pero eso puede hacer que el trazo desborde el relleno. Si acaricias, luego llenas, como en el bloque de abajo, obtienes un contorno visible detrás del texto legible.

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

Muchas gracias ... funcionó muy bien !! – user836026

+2

no funciona en mi caso, seguí los mismos pasos –

0

La respuesta aceptada no funcionaba para mí posiblemente porque drawAtPoint:withFont: está en desuso. Pude hacer que funcionara con el siguiente código:

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]; 
Cuestiones relacionadas