【问题标题】:How to draw only the outline of a text, but not the fill itself?如何只绘制文本的轮廓,而不是填充本身?
【发布时间】:2011-06-10 17:02:46
【问题描述】:

我正在使用这种方法在 -drawRect 中绘制文本:

[someText drawInRect:rect withFont:font lineBreakMode:someLineBreakMode alignment:someAlignment];

我只想画轮廓而不是填充!

我发现我可以设置CGContextSetFillColorWithColor 并提供完全透明的颜色。但我担心这会对性能产生不良影响,因为它可能会在幕后使用透明颜色完成所有繁重的绘图工作。

如果只需要轮廓图,有没有办法只禁用填充图?

【问题讨论】:

标签: iphone objective-c ios ipad core-graphics


【解决方案1】:

您是否尝试过使用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?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 2019-01-06
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多