2011-01-12 52 views
29

我想知道如何在iOS4中爲UILabel創建文本描邊?我需要一些建議。我想是這樣的:爲UILabel創建文本行程iphone

alt text

編輯:

UIFont *font = [UIFont fontWithName:@"Arial" size:222]; 
CGPoint point = CGPointMake(0,0); 

CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.7); 
CGContextSetRGBStrokeColor(context, 2, 2, 2, 1.0); 
CGContextSetTextDrawingMode(context, kCGTextFillStroke); 
CGContextSaveGState(context); 

// I change it to my outlet 
[label.text drawAtPoint:point withFont:font]; 

CGContextRestoreGState(context); 

回答

21
UIFont *font = [UIFont fontWithName:@"Arial" size:14]; 
CGPoint point = CGPointMake(0,0); 

CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.7); 
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0); 
CGContextSetTextDrawingMode(context, kCGTextFillStroke); 
CGContextSaveGState(context); 
[@"Label" drawAtPoint:point withFont:font]; 

CGContextRestoreGState(context); 

你可以看看這裏:

http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_text/dq_text.html

,並在這裏的示例代碼: http://developer.apple.com/library/ios/#samplecode/QuartzDemo/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40007531

+0

謝謝你,但什麼都沒有改變! ...是@「標籤」我的插座?對不起Iam新手在IOS編程,我把我的代碼放在viewDidLoad – 2011-01-12 08:12:27

0

好吧,如果你想用自定義字體(比如CGFontRef)在一個標籤中繪製文本,這並不是很簡單。我google了一下,找到了一個解決方案,這意味着你要繼承UILabel類併爲此重寫drawTextInRect方法。所有您需要的信息是here

1

我有一個乾淨的解決方案實現UILabelStroke子類:

@implementation UILabelStroked 
@synthesize strokeColor; 
- (void)drawTextInRect:(CGRect)rect { 

    UIColor *borderColor = self.strokeColor; 
    UIColor *fillColor = self.textColor; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetLineWidth(context, 1.0f); 
    CGContextSetTextDrawingMode(context, kCGTextStroke); 
    self.textColor = borderColor; 
    [super drawTextInRect:rect]; 

    CGContextSetLineWidth(context, 0.0f); 
    CGContextSetTextDrawingMode(context, kCGTextFillStroke); 
    self.textColor = fillColor; 
    [super drawTextInRect:rect]; 
} 
@end