2013-02-25 10 views
6

Ich versuche, ein Bild von Text zu erstellen, um über ein anderes Bild zu legen. Der Text muss eine weiße Fläche und eine schwarze Kontur haben. Ich benutze Objective-C und es ist für das iPhone. Ich kann den Text sehen lassen; Ich kann jedoch nicht herausfinden, wie man den Pinselstrich und die Füllfarbe ändert.Wie zeichne ich Text mit Attributen (speziell Strich-und Füllfarbe) in UIGraphicsImageContext für iPhone

self.bigImage ist ein Zeiger auf ein UIImage in meinem Xib.

Hier ist mein Code:

- (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; 
} 



ich bekommen kann es das eine oder andere zu machen (Dieser Code funktioniert ... Ich kann es einen schwarzen Strich mit weißer Füllung jedoch haben will) . Wenn ich diesen Code hinzufügen, erhalte ich einen schwarzen Strich:

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



Wenn ich diesen Code hinzufügen, erhalte ich ein weißer Schrift:

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

Aber wenn ich beide snipets hinzufügen erhalte ich die schwarz Schlaganfall, aber die Schriftfarbe ist transparent ...



Gelöst: dank ein wenig Hilfe von ‚Brane‘:

Ich brauchte dieses Stück Code vor dem drawAtPoint hinzuzufügen:

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

Antwort

3

ich dies durch Modifizieren Brane fixen Post ... Ich musste dieses Code-Snipet hinzufügen, bevor drawAtPoint ...

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

Zwei Probleme zu lösen.

Zuerst, um den Hintergrund zu zeichnen, können Sie nicht auf drawAtPoint:withFont: verlassen, da es Hintergrund nicht zeichnet.

Verwenden CGContextFillRect Ihre Hintergrundfarbe zeichnen:

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

Zweitens müssen Sie die Strichfarbe einstellen

[[UIColor blackColor] set]; 

vor mit dem Aufruf [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];

+0

ich suche nicht mit einem Hub von schwarzer Hintergrundfarbe weiß, aber die Schrift selbst weiß zu machen. Ich möchte, dass die Hintergrundfarbe transparent ist. – werdsackjon

8

Ich reparierte mit:

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

endlich !!!

dank levi

Levi

Verwandte Themen