iphone - 如何只绘制文本的轮廓,而不绘制填充本身?

标签 iphone objective-c ios ipad core-graphics

我使用以下方法在 -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/

相关文章:

iphone - 从 Objective-C 中的另一个类调用方法

ios - Objective C ScrollView 屏幕截图在 ios 13 中存在问题

ios - 如何访问 iOS 中的所有文件(pdf、doc 等)?

iphone - @implementation类别名称{}

ios - 我应该如何处理静态库中的依赖项?

ios - UITableViewCell 子类的行为不像 UITableViewCells

ios - 将 UIViewController 更改为 UITableViewController

ios - 如何使用 PFQuery 查询与数组的精确匹配?

ios - 无法在 Facebook 分享中添加标题和描述

ios - 显示 iOS 表情符号键盘