Reputation: 3068
I am going to implement the module for adding the numbers in WHITE color onto the arrow heads of the UIImage
being drawn onto the ios apps. When it comes to the execution, it shows that only black color is set on the the numbers. I have added
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [UIColor whiteColor].CGColor);
but to no avail. Would you please tell which is the better way to do so ?
The below is my working
-(UIImage*) drawText:(NSString*) text
inImage:(UIImage*) image
atPoint:(CGPoint) point
{
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
[[UIColor whiteColor] set];
UIFont *font = [UIFont boldSystemFontOfSize:24];
if([text respondsToSelector:@selector(drawInRect:withAttributes:)])
{
//IOS 7
NSDictionary *att = @{NSFontAttributeName:font};
[text drawInRect:rect withAttributes:att];
}
else
{
[text drawInRect:CGRectIntegral(rect) withFont:font];
}
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [UIColor whiteColor].CGColor);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Upvotes: 1
Views: 527
Reputation: 48514
Depending on how far back in time you want to go, pick any of these 3 solutions.
The 6+ and 7+ versions make use of NSForegroundColorAttributeName
to select the color. These are the preferred methods.
iOS 7+
// (add the color to the attribute)
NSDictionary *att = @{NSFontAttributeName :font,
NSForegroundColorAttributeName:[UIColor whiteColor]};
[@"test" drawInRect:rect withAttributes:att];
iOS 6+ (works in iOS 7 too)
// (add the color to the attribute, using NSAttributedString)
NSDictionary *att = @{NSFontAttributeName :font,
NSForegroundColorAttributeName:[UIColor whiteColor]};
NSAttributedString * text = [[NSAttributedString alloc] initWithString:@"test"
attributes:att];
[text drawInRect:rect];
iOS 5 (does not work on iOS 7)
// No attributes involved
[[UIColor whiteColor] set];
[@"test" drawInRect:CGRectIntegral(rect) withFont:font];
Upvotes: 2