开发者

How to draw only the outline of a text, but not the fill itself?

I'm drawing text in -drawRect with this method:

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

I just want to draw the outline but not the fill!

I've found that I can set CGContextSetFillColorWithColor and just provide an fully tran开发者_StackOverflow中文版sparent color. But I fear this has bad performance impact because probably it does all the heavy drawing work behind the scenes with a transparent color.

Is there a way to just disable the fill-drawing if only outline-drawing is wanted?


Have you tried using kCGTextFillStroke? This might work easily. To use it, just override 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;

}

EDIT: This answer also appears in a previous incarnation of this question: How do I make UILabel display outlined text?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜