I am trying to create an image of text to put on top of another image. The text needs to have a white fill and a black stroke outline. I am using objective-C and it is for the iPhone. I can get the text to show up; however, I cannot seam to figure out how to get the stroke and fill color to 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 black stroke with white fill however)
- (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 it to render one or the other. If I add this code I get a black stroke:
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 snipets I get the black stroke but the font color is transparent...
Solved: thanks to a little help from 'Brane':
I needed to add this bit of code before the drawAtPoint
:
CGFloat fontColor[4] = {255,255,255,1};
CGContextSetFillColor(UIGraphicsGetCurrentContext(), fontColor);
CGContextSetTextDrawingMode(UIGraphicsGetCurrentContext(), kCGTextFillStroke);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor blackColor] CGColor]);
I fixed with:
[theText drawAtPoint:CGPointMake(x, y)
withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:8], NSForegroundColorAttributeName:[UIColor whiteColor] }];
finally!!!
thanks to levi
Levi
Two problems to solve.
First, in order to draw the background, you can't rely on drawAtPoint:withFont:
as it does not draw background.
Use CGContextFillRect to draw your background color:
[[UIColor whiteColor] set];
CGContextFillRect( UIGraphicsGetCurrentContext(), CGRectMake( 0, 0, size.width, size.height )
Second, you need to set the stroke colour using
[[UIColor blackColor] set];
prior to calling [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];
I fixed this by modifying Brane's post... I needed to add this snipet 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]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With