2011-01-12 16 views
29

Mi chiedevo come posso creare un tratto di testo per UILabel in iOS4? Ho bisogno di qualche suggerimento. Voglio qualcosa di simile:Crea testo Tratto per UILabel iphone

alt text

Modificato:

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); 

risposta

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); 

si può guardare qui:

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

e nel codice di esempio qui: http://developer.apple.com/library/ios/#samplecode/QuartzDemo/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40007531

+0

ringraziamento, ma nulla è cambiato! ... è @ "Etichetta" la mia presa? scusa Iam novizio in programmazione IOS, ho messo il mio codice su viewDidLoad –

+0

puoi pubblicare il tuo codice? – shannoga

+0

@ "Lable" è solo una stringa, il testo che si desidera stampare – FoJjen

0

Bene, se si desidera disegnare un testo in un'etichetta con un carattere personalizzato, come CGFontRef, non è abbastanza semplice. Ho cercato un po 'su Google e ho trovato una soluzione per te che implica l'ereditarietà della classe UILabel e la riscrittura del metodo drawTextInRect per questo. Tutte le informazioni necessarie per te sono here.

1

ho una soluzione pulita attuazione UILabelStroke sottoclasse:

@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