Iphone 如何仅绘制文本的轮廓,而不绘制填充本身?
我使用以下方法在-drawRect中绘制文本:Iphone 如何仅绘制文本的轮廓,而不绘制填充本身?,iphone,objective-c,ios,ipad,core-graphics,Iphone,Objective C,Ios,Ipad,Core Graphics,我使用以下方法在-drawRect中绘制文本: [someText drawInRect:rect withFont:font lineBreakMode:someLineBreakMode alignment:someAlignment]; 我只想画轮廓,不想画填充 我发现我可以设置CGContextSetFillColorWithColor,只需提供完全透明的颜色。但我担心这会对性能造成不良影响,因为它可能会以透明的颜色在幕后完成所有繁重的绘图工作 如果只需要轮廓图,有没有办法禁用填充图?
[someText drawInRect:rect withFont:font lineBreakMode:someLineBreakMode alignment:someAlignment];
我只想画轮廓,不想画填充
我发现我可以设置CGContextSetFillColorWithColor
,只需提供完全透明的颜色。但我担心这会对性能造成不良影响,因为它可能会以透明的颜色在幕后完成所有繁重的绘图工作
如果只需要轮廓图,有没有办法禁用填充图?您是否尝试过使用
kCGTextFillStroke
?这可能很容易奏效。要使用它,只需覆盖drawTextInRect
- (void)drawTextInRect:(CGRect)rect {
CGSize shadowOffset = self.shadowOffset;
UIColor *textColor = self.textColor;
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, 1);
CGContextSetLineJoin(c, kCGLineJoinRound);
CGContextSetTextDrawingMode(c, kCGTextStroke);
self.textColor = [UIColor whiteColor];
[super drawTextInRect:rect];
CGContextSetTextDrawingMode(c, kCGTextFill);
self.textColor = textColor;
self.shadowOffset = CGSizeMake(0, 0);
[super drawTextInRect:rect];
self.shadowOffset = shadowOffset;
}
编辑:这个答案也出现在这个问题的前一个版本中:可能重复的