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