2012-04-05 4 views
6

境界線(アウトライン)に別の色のテキストを表示したいとします。 私はテキストフォントをどのようにアウトラインできますか?

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

を使用してMapOverlayViewでテキストを表示しようとしている私はアウトラインを表示するテキストを必要とする以外は正常に動作します。

答えて

8

あなたはおそらくそれが良い見えるようにいくつかの数字や色を調整する必要がありますが、はい、あなたは、CGContextSetDrawingMode(CGContextRef, CGTextDrawingMode)の助けを借りて概説テキストを表示することができます。それが作る

はkCGTextFillStrokeを使用して論理的に見えるが、それはストロークが塗りつぶしを圧倒することができます。ストロークした場合は、下のブロックのように塗りつぶして、読みやすいテキストの目に見えるアウトラインを取得します。

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

多くのありがとう...それは素晴らしい! – user836026

+2

私の場合は動作しません私は同じ手順に従った –

0

drawAtPoint:withFont:が推奨されていませんので、受け入れ答えはおそらく私のために動作しませんでした。私はそれを次のコードで動作させることができました:

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]; 
関連する問題