2011-06-10 94 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?