2012-04-05 69 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 2012-04-10 13:50:13

+2

在我的情况下不工作我遵循相同的步骤 – 2013-03-07 07:08:51

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