2

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 transparent 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?

Proud Member
  • 40,078
  • 47
  • 146
  • 231
  • possible duplicate of [How do I make UILabel display outlined text?](http://stackoverflow.com/questions/1103148/how-do-i-make-uilabel-display-outlined-text) – PengOne Jun 10 '11 at 17:13

1 Answers1

3

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?

Community
  • 1
  • 1
PengOne
  • 48,188
  • 17
  • 130
  • 149