2012-04-05 11 views
6

Vorrei visualizzare il testo con un altro colore nel suo bordo (struttura). Sto cercando di visualizzare un testo in MapOverlayView utilizzandoCome posso delineare il carattere del testo?

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

funziona bene, tranne che ho bisogno di testo di essere visualizzati contorni.

risposta

8

Sì, è possibile visualizzare il testo delineato con l'ausilio di CGContextSetDrawingMode(CGContextRef, CGTextDrawingMode), anche se probabilmente sarà necessario regolare alcuni numeri e colori per renderlo bello.

Sembra logico utilizzare kCGTextFillStroke, ma questo può far sì che il tratto sommerga il riempimento. Se si preme, quindi riempire, come nel blocco sottostante, si ottiene un contorno visibile dietro il testo leggibile.

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

Grazie mille ... ha funzionato benissimo !! – user836026

+2

non funziona nel mio caso ho seguito gli stessi passaggi –

0

La risposta accettata non ha funzionato per me forse perché drawAtPoint:withFont: è deprecato. Sono stato in grado di farlo funzionare con il seguente codice:

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]; 
Problemi correlati