Ios 我怎样才能勾勒出文字字体的轮廓?

Ios 我怎样才能勾勒出文字字体的轮廓?,ios,Ios,我想用另一种颜色显示文本的边框(轮廓)。 我正在尝试使用在MapOverlayView中显示文本 [text drawAtPoint:CGPointMake(0,30) withFont:[UIFont fontWithName:@"Helvetica-Bold" size:(3 * MKRoadWidthAtZoomScale(zoomScale))] 它工作正常,只是我需要文本显示轮廓。是的,您可以借助CGContextSetDrawingMode(CGContextRef,CGText

我想用另一种颜色显示文本的边框(轮廓)。 我正在尝试使用在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];