I have to add a small red dot to a UIButton. I set the color of custom UIButton using this method:
+ (UIImage *) imageFromColor:(UIColor *)color {
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
// [[UIColor colorWithRed:222./255 green:227./255 blue: 229./255 alpha:1] CGColor]) ;
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Then I have just to add a simple red dot to this button which is assumably done like this:
+ (UIImage *) addRedDot
{
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
// [[UIColor colorWithRed:222./255 green:227./255 blue: 229./255 alpha:1] CGColor]) ;
CGContextSetRGBStrokeColor(context, 1.0f, 0.0f, 0.0f, 1.0f);
CGContextSetLineWidth(context, 2.0);
CGContextStrokeEllipseInRect(context, CGRectMake(10, 11, 21, 21));
// CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
But it just returns a white image..
this client code is:
if ([dateStringToAssign isEqualToString:[self dateToString:[NSDate date]]])
{
[buttonToLay setBackgroundImage:[CommonUIUtility imageFromColor:[UIColor cyanColor]]
forState:UIControlStateNormal];
}
if([dbm hasEventsForDate:buttonToLay.ownedDate])
{
NSLog(@"Has events");
[buttonToLay setBackgroundImage:[CommonUIUtility addRedDot] forState:UIControlStateNormal];
}
What’s the right way to add a small red dot to existing CGContext?
You’re stroking an ellipse beyonds the bounds of the context you set up in this line:
CGContextStrokeEllipseInRect(context, CGRectMake(10, 11, 21, 21));In order to have the ellipse show up, it needs to be within the bounds of your context, which appears to have size 1×1.