我使用以下方法在 -drawRect 中绘制文本:
[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;
}
编辑:这个答案也出现在这个问题的前一个版本中:How do I make UILabel display outlined text?
关于iphone - 如何只绘制文本的轮廓,而不绘制填充本身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6309713/