8 Apr 2017

Updated A Simple Calendar Creation with Present day Highlighted...


Updated A Simple Calendar Creationwith Present day Highlighted...


Storyboard:-


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *previousBtn;
- (IBAction)previousBtn:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
- (IBAction)nextBtn:(id)sender;

@end


#import "ViewController.h"



@interface ViewController ()


{
    NSArray *daysArr;
    UIImageView *imgView;
    UIButton *btn;
    NSString *dateComponents;

    NSString *temStr;
    NSString *monthTemp;
    NSString *yearTemp;
    __weak IBOutlet UILabel *monthNameLbl;
    __weak IBOutlet UILabel *monthLbl;
    __weak IBOutlet UILabel *yearLbl;
    __weak IBOutlet UILabel *selectedDateLbl;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
   NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
    
    monthLbl.hidden = YES;
    monthLbl.text = [NSString stringWithFormat: @"%ld", (long)[components1 month]];
    yearLbl.text = [NSString stringWithFormat: @"%ld", (long)[components1 year]];

    monthTemp = monthLbl.text;
    yearTemp = yearLbl.text;

    monthNameLbl.textAlignment = 1;
    int monthNumber = [components1 month];
    NSDateFormatter *df = [[NSDateFormatter alloc] init] ;
    NSString *monthName = [[df monthSymbols] objectAtIndex:(monthNumber-1)];
    monthNameLbl.text = monthName;
    
   daysArr = [[NSArray alloc]initWithObjects:@"Mon",@"Tue",@"Wed",@"Thu",@"Fri",@"Sat",@"Sun", nil];
    
    int xOffSet = 8;
    int yOffSet = 118;
    for (int i=1; i<=7; i++) {
        UILabel *lblForDay=[[UILabel alloc]initWithFrame: btn.frame = CGRectMake(xOffSet,yOffSet , ((self.view.frame.size.width-8)/7)-3, 30)];
        lblForDay.text=[NSString stringWithFormat:@"%@",[daysArr objectAtIndex:i-1]];
        lblForDay.textColor = [UIColor redColor];
        lblForDay.font = [UIFont fontWithName:@"Verdana" size:13];
        lblForDay.backgroundColor = [UIColor purpleColor];
        lblForDay.textAlignment = 1;
        [self.view addSubview:lblForDay];
        if (i%7==0) {
            xOffSet=8;
            yOffSet+=50;
        }else{
            xOffSet+=((self.view.frame.size.width-8)/7)-1;
        }
    }
    [self generateCalendar];
}

- (void)removeAllSubviews
{
    for (UIView *subview in self.view.subviews)
        
        if([subview isMemberOfClass:[UIButton class]])
        {
            if ([subview tag] == 101 || [subview tag] == 102 ||[subview tag] == 103)
            NSLog(@"tag 101 generate btn not removed");
            else
            [subview removeFromSuperview];
        }
}

-(void) generateCalendar {
    
    [self removeAllSubviews];
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone* timeZone = [NSTimeZone localTimeZone];
    [gregorian setTimeZone:timeZone];
    
    NSString* timeZoneName = [timeZone localizedName:NSTimeZoneNameStyleStandard locale:[NSLocale currentLocale]];
    NSLog(@"timeZoneName : %@", timeZoneName);
    
    NSDateComponents *components = [[NSDateComponents alloc] init] ;
  
   // Set your year and month here
    [components setMonth:monthLbl.text.integerValue];
    [components setYear:yearLbl.text.integerValue];
    
        NSDate *date = [gregorian dateFromComponents:components];
    
        NSRange days = [gregorian rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    
        NSDate *firstDayOfMonthDate = [gregorian dateFromComponents:components];
        NSLog(@"firstDayOfMonthDate %@",firstDayOfMonthDate);
    
    
//    NSDateComponents *components = [[NSCalendar currentCalendar] components: NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
//  
//    NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:components];
//    
//    NSRange days = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay
//                                       inUnit:NSCalendarUnitMonth
//                                      forDate:date];
//    NSDate *firstDayOfMonthDate = [[NSCalendar currentCalendar] dateFromComponents:components];

    NSCalendar *cal1=[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps1 = [cal1 components:NSCalendarUnitWeekday fromDate:firstDayOfMonthDate];
    // 1 for mon 7 for sun
    long  int startWithDay=[comps1 weekday]==1?7:[comps1 weekday]-1;
    NSLog(@"day numbr of first date of month is(startWithDay) %ld",startWithDay);
    
    int x=8;
    int y=150;
    int d;
    for (d=1;d<days.length+startWithDay;d++) {
        if ((d-startWithDay) >=0) {
            NSLog(@"tag is %ld",d-startWithDay+1);
            
            btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn addTarget:self action:@selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag=d-startWithDay+1;
            btn.frame = CGRectMake(x, y, ((self.view.frame.size.width-8)/7)-3, 48);
            //btn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
            btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
            btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            [btn setTitle:[NSString stringWithFormat:@"%ld",d-startWithDay+1] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor blueColor];


if ([monthLbl.text isEqualToString:monthTemp] && [yearLbl.text isEqualToString:yearTemp] ) 
{
NSDate *todayDate = [NSDate date];
            if (btn.titleLabel.text.integerValue > 9) {
               dateComponents = [[NSString stringWithFormat:@"%@",todayDate] substringWithRange:NSMakeRange(8, 2)];
                
            }else{
               dateComponents = [[NSString stringWithFormat:@"%@",todayDate] substringWithRange:NSMakeRange(9, 1)];
            }
           
            if ([btn.titleLabel.text isEqualToString:dateComponents]) {
                temStr = dateComponents;
                btn.backgroundColor = [UIColor greenColor];

            }

       }

            [self.view addSubview:btn];
            [btn setUserInteractionEnabled:YES];
            
        }else{
            NSLog(@"next line %d",d);
        }
        
        if (d%7==0) {
            x=8;
            y+=50;
        }else{
            x+=((self.view.frame.size.width-8)/7)-1;
        }
    }
}

- (void)tapped:(UIButton *)sender
{
     if ([sender isSelected]) {
        [sender setSelected: NO];


if ([sender.titleLabel.text isEqualToString:temStr]) {
             sender.backgroundColor = [UIColor greenColor];
         }else {
             sender.backgroundColor = [UIColor blueColor];

         }
        NSString *selectedDate = [NSString stringWithFormat:@"Your DeSelected %ld/%d/%d",(long)sender.tag,monthLbl.text.integerValue,yearLbl.text.integerValue];
         selectedDateLbl.text = selectedDate;
        
    } else {
        [sender setSelected: YES];
         sender.backgroundColor = [UIColor orangeColor];
        
        NSString *selectedDate = [NSString stringWithFormat:@"Your Selected %ld/%d/%d",(long)sender.tag,monthLbl.text.integerValue,yearLbl.text.integerValue];
        selectedDateLbl.text = selectedDate;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)previousBtn:(id)sender {
    
    if (monthLbl.text.integerValue-1 < 1) {
        monthLbl.text = [NSString stringWithFormat:@"%d",12];
        yearLbl.text = [NSString stringWithFormat:@"%d",yearLbl.text.integerValue-1];
    }else {
        monthLbl.text = [NSString stringWithFormat:@"%d",monthLbl.text.integerValue-1];
    }
    
    int monthNumber =monthLbl.text.integerValue;
    NSDateFormatter *df = [[NSDateFormatter alloc] init] ;
    NSString *monthName = [[df monthSymbols] objectAtIndex:(monthNumber-1)];
    monthNameLbl.text = monthName;

    temStr  =@"";
    [self generateCalendar];
}

- (IBAction)nextBtn:(id)sender {
    
    if (monthLbl.text.integerValue+1 > 12) {
        monthLbl.text = [NSString stringWithFormat:@"%d",1];
        yearLbl.text = [NSString stringWithFormat:@"%d",yearLbl.text.integerValue+1];
    }else {
     monthLbl.text = [NSString stringWithFormat:@"%d",monthLbl.text.integerValue+1];
    }
    
    int monthNumber =monthLbl.text.integerValue;
    NSDateFormatter *df = [[NSDateFormatter alloc] init] ;
    NSString *monthName = [[df monthSymbols] objectAtIndex:(monthNumber-1)];
    monthNameLbl.text = monthName;

    temStr  =@"";
    [self generateCalendar];
}
@end


Result:-

  








No comments:

Post a Comment

Recent Posts

Codable demo

Link: https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0