Custom Calendar on iPhone, iPad(Objective C)

Custom Calendar on iPhone, iPad(Objective C)

Iphone Custom Calendar

There are many development related iPhone apps , but it is not very often you come across an app that is useful and works as it desired. Therefore, here is Customized Calendar in Iphone that will be useful as an iPhone developer. It provides a similar look and feel as on a portal.

Our requirement includes to display a calendar when tapped on respective UITextField, by which we can view dates with its month, year and weekday. This Customized Calendar also includes the feature to switch previous/next month and previous/next year.

Though up to certain extent Calendar class can be achieved by using UIPickerView, but PickerView does not permit fast switching to previous/next month and year.

First, create an event on textfield click, this event then uses UIPopoverController where calendar View exists.

Then , find total number of days in a particular month with respect to its year and then the weekday for first date of that month. Total number of days gives the idea about how many date UIImageView will be created and he weekday for first date of that month gives the location from where the calendar dates will start. ImageView are placed through loop till number of days of that month.

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
[comp setDay:1];
NSDate *firstDayOfMonthDate = [gregorian dateFromComponents:comp];
NSLog(@"firstDayOfMonthDate %@",firstDayOfMonthDate);

NSCalendar *cal1=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps1 = [cal1 components:NSWeekdayCalendarUnit fromDate:firstDayOfMonthDate];
// 1 for mon 7 for sun
int startWithDay=[comps1 weekday]==1?7:[comps1 weekday]-1;
NSLog(@"day numbr of first date of month is(startWithDay) %d",startWithDay);

Each ImageView is given with UITapGestureRecognizer, which is called when tapped on particular date. All ImageView are given with tag , this tag then appends with month and year at label and forms date as desired in required date formatter.

int x=0;
int y=100;
for (int d=1; d
if (d-startWithDay>=0) {
// NSLog(@"tag is %d",d-startWithDay+1);
imgView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"weeklyTableDetails.png"]];
imgView.tag=d-startWithDay+1;
imgView.frame=CGRectMake(x, y, 57, 50);
[imgView addGestureRecognizer:tap];
[self.view addSubview:imgView];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getTag:)];
[imgView setUserInteractionEnabled:YES];
[imgView addGestureRecognizer:tap];

lblForDate=[[UILabel alloc]initWithFrame:CGRectMake(20, 15, 30, 30)];
lblForDate.text=[NSString stringWithFormat:@"%d",d-startWithDay+1];
lblForDate.textColor = [UIColor colorWithRed:0/255.f green:130/255.f blue:184/255.f alpha:1];
lblForDate.font = [UIFont fontWithName:@"Verdana" size:15];
lblForDate.backgroundColor = [UIColor clearColor];
[imgView addSubview:lblForDate];
}else{
NSLog(@"next line %d",d);
}
if (d%7==0) {
x=0;
y+=50;

}else{
x+=57;
}

And this is how each date image of size 57*50 is generated according to respective month and year.

0 comments:

Post a Comment