Monday 22 June 2015

Add a Calendar into iOS - CKCalenderView

Create a ViewController File for Calendar. In Framework File use to customise the functions & Values in iOS [You can set the Minimum date, Enable dates, Set Images & More].

Add the Frameworks: UIKit, Foundation, CoreGraphics, QuartzCore

Download or copy the Two Files. Added code into your project.
CKCalenderView.h 
CKCalenderView.m 

Download Sample Code
(or)


In Framework File - Add the Code for CKCalenderView.h : #import <UIKit/UIKit.h>

CalenderViewController.h
#import <UIKit/UIKit.h>

@interface CalenderViewController : UIViewController
{
    IBOutlet UITableView *table_view;
}

@end


CalenderViewController.m
#import <CoreGraphics/CoreGraphics.h>
#import "CKCalendarView.h"

@interface CalenderViewController ()<CKCalendarDelegate>
{
    NSMutableArray *Dates;
}

@property(nonatomicweakCKCalendarView *calendar;
@property(nonatomicstrongUILabel *dateLabel;
@property(nonatomicstrongNSDateFormatter *dateFormatter;
@property(nonatomicstrongNSDate *minimumDate;
@property(nonatomicstrongNSMutableArray *disabledDates;

@end

@implementation CalenderViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @“Calendar Days";
    
    CKCalendarView *calendar = [[CKCalendarView allocinitWithStartDay:startSunday];
    self.calendar = calendar;
    calendar.delegate = self;
    //self.calendar.backgroundColor = [UIColor blueColor];
    
    self.dateFormatter = [[NSDateFormatter allocinit];
    [self.dateFormatter setDateFormat:@"dd/MM/yyyy"];
    self.minimumDate = [self.dateFormatter dateFromString:@"20/09/2014"];
    
    self.disabledDates = [[NSMutableArray alloc]init];
    
    Dates = [[NSMutableArray alloc]initWithObjects:@"22/12/2014"@"25/12/2014"@"29/12/2014",  @"01/01/2015"@"21/01/2015"@"15/04/2015"nil];
    
    for (int i = 0; i<[Dates count]; i++)
    {
        NSString *str_date = [Dates objectAtIndex:i];
        [self.disabledDates addObject:[self.dateFormatter dateFromString:str_date]];
    }
    
    calendar.onlyShowCurrentMonth = YES;
    calendar.adaptHeightToNumberOfWeeksInMonth = NO;
    
    calendar.frame = CGRectMake(1080300320);
    [self.view addSubview:calendar];
    
    self.dateLabel = [[UILabel allocinitWithFrame:CGRectMake(10CGRectGetMaxY(calendar.frame) + 4self.view.bounds.size.width24)];
    [self.view addSubview:self.dateLabel];
    
    self.view.backgroundColor = [UIColor whiteColor];
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenterremoveObserver:self];
}


- (void)localeDidChange
{
    [self.calendar setLocale:[NSLocale currentLocale]];
}


- (BOOL)dateIsDisabled:(NSDate *)date
{
    for (NSDate *disabledDate in self.disabledDates)
    {
        if ([disabledDate isEqualToDate:date])
        {
            return YES;
        }
    }
    return NO;
}


#pragma mark -
#pragma mark - CKCalendarDelegate

- (void)calendar:(CKCalendarView *)calendar configureDateItem:(CKDateItem *)dateItem forDate:(NSDate *)date
{
    // TODO: play with the coloring if we want to...
    if ([self dateIsDisabled:date])
    {
        dateItem.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"red.png"]];
        dateItem.textColor = [UIColor whiteColor];
    }
}


- (BOOL)calendar:(CKCalendarView *)calendar willSelectDate:(NSDate *)date
{
    return ![self dateIsDisabled:date];
}


- (void)calendar:(CKCalendarView *)calendar didSelectDate:(NSDate *)date
{
    self.dateLabel.text = [self.dateFormatter stringFromDate:date];
}


- (BOOL)calendar:(CKCalendarView *)calendar willChangeToMonth:(NSDate *)date
{
    if ([date laterDate:self.minimumDate] == date)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}


- (void)calendar:(CKCalendarView *)calendar didLayoutInRect:(CGRect)frame
{
    NSLog(@"calendar layout: %@"NSStringFromCGRect(frame));
}


#pragma mark -
#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [Dates count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell allocinitWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    
    cell.textLabel.text = [Dates objectAtIndex:indexPath.row];
    return cell;
}

@end

No comments: