OGeek|极客世界-中国程序员成长平台

标题: iphone - 如何只绘制文本的轮廓,而不是填充本身? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 07:58
标题: iphone - 如何只绘制文本的轮廓,而不是填充本身?

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

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

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

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

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



Best Answer-推荐答案


您是否尝试过使用 kCGTextFillStroke?这可能很容易工作。要使用它,只需覆盖 drawTextInRect

- (void)drawTextInRectCGRect)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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4