2011-06-10 86 views
2

我画文字-drawRect用这种方法:如何仅绘制文本的轮廓,而不绘制填充本身?

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

我只是想画出轮廓,但不补!

我发现我可以设置CGContextSetFillColorWithColor并只提供一个完全透明的颜色。但我担心这会对性能造成不良影响,因为它可能会以透明色彩在幕后进行所有沉重的绘画工作。

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

+0

可能重复(HTTP [我如何的UILabel显示概述文本?]:/ /stackoverflow.com/questions/1103148/how-do-i-make-uilabel-display-outlined-text) – PengOne 2011-06-10 17:13:10

回答

3

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