我怎么能概述文字字体?

我想在其边框(轮廓)中显示另一种颜色的文本。 我正在尝试使用MapOverlayView显示文本

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

它工作正常,除了我需要文本显示轮廓。

是的,您可以借助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]; 

接受的答案对我不起作用,因为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];