How to draw text with attributes (in particular, stroke and fill color) in UIGraphicsImageContext for iPhone

I am trying to create an image of text to put on top of another image. The text should have a white fill and a black hatch pattern. I use objective-C, and this is for the iPhone. I can get the text to display; however, I cannot figure out how to get the stroke and fill the color for the change.

self.bigImage is a pointer to a UIImage in my xib.

Here is my code: (This code works ... I want it to have a back door with white fill)

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




I can get him to do one or the other. If I add this code, I get a backdoor:

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




If I add this code, I get a white font:

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

But if I add both snipers, I get a back door, but the font color is transparent ...




Solved: thanks to a little help from Branagh:

I needed to add this bit of code before drawAtPoint :

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

I fixed this by changing Brane's message ... I needed to add this snape code before drawAtPoint ...

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

I've corrected:

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

finally!!!

thanks levi

Levy

+8
source

Two problems to solve.

First, to draw a background, you cannot rely on drawAtPoint:withFont: since it does not draw a background.

Use CGContextFillRect to draw the background color:

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

Secondly, you need to set the stroke color with

 [[UIColor blackColor] set]; 

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

+3
source

All Articles