Search This Blog

Wednesday, December 29, 2010

Generating runtime image in iPhone/ iPad

input color in int.

+ (UIImage *)shapeCircle:(int)color {

// create the bitmap context
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(nil,30,30,8,0, colorSpace,kCGImageAlphaPremultipliedLast);

// set the fill color
//CGColorRef fillColor = [[UIColor blackColor] CGColor];
NSString *hexColor = [NSString stringWithFormat:@"%x", color];
hexColor = [[@"" stringByPaddingToLength:6 - [hexColor length] withString:@"0" startingAtIndex:0] stringByAppendingString:hexColor];
float r = [self hexToColorValue:[hexColor substringToIndex:2]];
float g = [self hexToColorValue:[hexColor substringWithRange:NSMakeRange(2, 2)]];
float b = [self hexToColorValue:[hexColor substringFromIndex:4]];

//fillColor = [[[UIColor alloc] initWithRed:1 green:0 blue:0 alpha:1] CGColor];
//CGContextSetFillColor(context, CGColorGetComponents(fillColor));
CGContextSetRGBFillColor(context, r, g, b, 1);
CGContextAddArc(context, 15.0f, 15.0f, 12.0f, 0, 360 * M_PI/180, 1);

//CGContextSetFillColor(context, CGColorGetComponents([[UIColor whiteColor] CGColor]));
CGContextSetRGBFillColor(context, 1, 1, 1, 1);
CGContextAddArc(context, 15.0f, 15.0f, 6.0f, 0, 360 * M_PI/180, 1);

// convert the context into a CGImageRef
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];

return [image autorelease];

+ (float)hexToColorValue:(NSString *)color {
NSScanner* scanner = nil;
unsigned int cInt;
scanner = [NSScanner scannerWithString:color];
[scanner scanHexInt:&cInt];
return (float)cInt / 255;

Adding a toolbar to keyboard for textField and textView

textField and textView have a property inputAccessoryView which allows you to easily add a toolbar to the top of the keyboard to show your custom buttons.

- (IBAction)beginEditing: (id)sender {
CGFloat _width = self.view.frame.size.width;
CGFloat _height = 40.0;

// Create a new toolbar
UIToolbar *toolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, -_height, _width, _height)] autorelease];
// Create the necessary buttons
UIBarButtonItem *barButtonItemSubmit = [[[UIBarButtonItem alloc] initWithTitle:@"Submit" style:UIBarButtonItemStyleBordered target:self action:@selector(btnSubmitClicked:)] autorelease];
UIBarButtonItem *barButtonItemCancel = [[[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(btnCancelClicked:)] autorelease];
By default the buttons that are added to the toolbar are left aligned.
To align them as you like, create a flexible space bar button and add it where you want.
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
Adding the flexible space in between will make both buttons float towards the opposite ends.
You can repeat the flexible space as many times and at positions to get the desired layout of buttons.
[toolbar setItems:[[[NSArray alloc] initWithObjects:barButtonItemSubmit, flexibleSpace, barButtonItemCancel, nil] autorelease]];

if ([sender isEqual:myTextField]) {
myTextField.inputAccessoryView = toolbar;
else if ([sender isEqual:myTextView]) {
myTextView.inputAccessoryView = toolbar;

Assign the begin editing event of the text field to the above function.

For textview, you might need to do something like this:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
if ([textView isEqual:myTextView]) {
[self beginEditing:myTextView];
return YES;

resignFirstResponder does not hide keyboard in presentModalViewController

For some great "Apple" reason, when the input elements are in a modal view controller, resignFirstResponder just loses the focus from the element. The keyboard stays visible.

After searching for a long time, I found 2 explanations for the same.
1. Its a bug!
2. Its a feature! Apple kept it that way to avoid the keyboard coming up and going down when switching between input elements in the modal form. B******T!

So the workaround I came up with is:
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
NSArray *arrWindows = [[UIApplication sharedApplication] windows];

if ([arrWindows count] > 1) {
UIWindow *keyboardWindow = [arrWindows objectAtIndex:1];
[keyboardWindow setHidden:YES];
return YES;