2013-02-25 14 views
6

Sto cercando di creare un'immagine di testo da mettere sopra un'altra immagine. Il testo deve avere un riempimento bianco e un contorno nero. Sto usando l'obiettivo-C ed è per l'iPhone. Posso far apparire il testo; tuttavia, non riesco a cucire per capire come ottenere il tratto e riempire il colore per cambiare.Come disegnare il testo con attributi (Specificamente tratto e colore di riempimento) in UIGraphicsImageContext per iPhone

self.bigImage è un puntatore a un UIImage nel mio xib.

Ecco il mio codice: (Questo codice funziona ... voglio che abbia un tratto nero con bianco per il riempimento però)

- (IBAction)submitBtn:(id)sender { 

    UIFont *myfont = [UIFont fontWithName:@"myfont" size:32]; 

    UIImage *textImage = [self imageFromText:@"teststring" font:myfont]; 
    CGSize finalSize = [self.bigImage.image size]; 
    CGSize textSize = [textImage size]; 

    UIGraphicsBeginImageContext(finalSize); 
    [self.bigImage.image drawInRect:CGRectMake(0,0, finalSize.width, finalSize.height)]; 
    [textImage drawInRect:CGRectMake(0, 0, textSize.width, textSize.height)]; 

    UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    self.bigImage.image = newImg; 
} 

-(UIImage *)imageFromText:(NSString *)text font:(UIFont *)font { 
    CGSize size = [text sizeWithFont:font]; 

    // check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+) 
    if (UIGraphicsBeginImageContextWithOptions != NULL) 
     UIGraphicsBeginImageContextWithOptions(size,NO,0.0); 
    else 
     // iOS is < 4.0 
     UIGraphicsBeginImageContext(size); 

    [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font]; 

    // transfer image 
    UIImage *Image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return Image; 
} 



posso farlo per rendere uno o l'altro . Se posso aggiungere questo codice ottengo un tratto nero:

CGContextSetTextDrawingMode(UIGraphicsGetCurrentContext(), kCGTextStroke); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2); 
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor blackColor] CGColor]); 



Se posso aggiungere questo codice ottengo un testo bianco:

CGFloat fontColor[4] = {255,255,255,1}; 
CGContextSetFillColor(UIGraphicsGetCurrentContext(), fontColor); 

Ma se aggiungo entrambi snipets ottengo il nero ictus, ma il colore del carattere è trasparente ...



Risolto: grazie ad un piccolo aiuto da 'Brane':

avevo bisogno di aggiungere questo pezzo di codice prima della drawAtPoint:

CGFloat fontColor[4] = {255,255,255,1}; 
CGContextSetFillColor(UIGraphicsGetCurrentContext(), fontColor); 
CGContextSetTextDrawingMode(UIGraphicsGetCurrentContext(), kCGTextFillStroke); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2); 
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor blackColor] CGColor]); 

risposta

3

Ho fissato questo modificando Brane di post ... Ho dovuto aggiungere questo snipet di codice prima dello drawAtPoint ...

CGFloat fontColor[4] = {255,255,255,1}; 
CGContextSetFillColor(UIGraphicsGetCurrentContext(), fontColor); 
CGContextSetTextDrawingMode(UIGraphicsGetCurrentContext(), kCGTextFillStroke); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2); 
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor blackColor] CGColor]); 
3

due problemi da risolvere.

Innanzitutto, per disegnare lo sfondo, non è possibile fare affidamento su drawAtPoint:withFont: in quanto non disegna lo sfondo.

Usa CGContextFillRect per disegnare il colore di sfondo:

[[UIColor whiteColor] set]; 
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, size.width, size.height) 

In secondo luogo, è necessario impostare il colore della traccia utilizzando

[[UIColor blackColor] set]; 

prima di chiamare [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];

+0

non sto cercando di fare il colore di sfondo bianco, ma il tipo di carattere in sé bianca con un colpo di nero. Voglio che il colore dello sfondo sia trasparente. – werdsackjon

8

ho fissato con:

[theText drawAtPoint:CGPointMake(x, y) 
        withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:8], NSForegroundColorAttributeName:[UIColor whiteColor] }]; 

finalmente !!!

grazie a Levi

Levi

Problemi correlati