Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change the first day of the week to monday in week view #313

Open
ranjith1109 opened this issue Dec 9, 2016 · 2 comments
Open

How to change the first day of the week to monday in week view #313

ranjith1109 opened this issue Dec 9, 2016 · 2 comments

Comments

@ranjith1109
Copy link

ranjith1109 commented Dec 9, 2016

Hi,

I have changed the first day of the week to monday in the month view but am not able to do the same in the week view.I changed to monday by using the following syntax " self.monthView = [self.monthView initWithSundayAsFirst:NO] " .How to achieve this in weekview? Kindly guide me.Thank you.

@ranjith1109
Copy link
Author

ranjith1109 commented Dec 16, 2016

Hi i fixed the above issue by editing two functions as below
This is to fix the day name :

- (void) _setupView{
    	self.nowLineView = [[TKNowView alloc] init];
	NSDateComponents *info = [[NSDate date] dateComponentsWithTimeZone:self.calendar.timeZone];
    info.weekday = 2;
	info.hour = info.minute = info.second = 0;
	self.formatter = [[NSDateFormatter alloc] init];
    self.formatter.timeZone = self.calendar.timeZone;
	self.eventGraveYard = [NSMutableArray array];
    self.backgroundColor = [UIColor whiteColor];
    self.clipsToBounds = YES;
    self.pages = [NSMutableArray arrayWithCapacity:3];
    self.weekdayPages = [NSMutableArray arrayWithCapacity:3];
    [self addSubview:self.horizontalScrollView];
	info.day -= 1;
	for(NSInteger i=0;i<3;i++){
		NSDate *date = [NSDate dateWithDateComponents:info];
		CGRect r = CGRectInset(self.horizontalScrollView.bounds, HORIZONTAL_PAD, 0);
		r.origin.x = CGRectGetWidth(self.horizontalScrollView.frame) * i + HORIZONTAL_PAD;
		r.origin.y = 0;
		CGRect rr = r;
		rr.origin.x = 0;
		UIScrollView *sv = [[UIScrollView alloc] initWithFrame:r];
		sv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
		sv.clipsToBounds = NO;
		sv.tag = i;
		sv.contentSize = CGSizeMake(0, TIMELINE_HEIGHT);
		sv.alwaysBounceVertical = TRUE;
		sv.backgroundColor = [UIColor whiteColor];
		[self.horizontalScrollView addSubview:sv];
		TKTimelineView *timelineView = [[TKTimelineView alloc] initWithFrame:sv.bounds];
		timelineView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
		[sv addSubview:timelineView];
		timelineView.date = date;
		[self.pages addObject:sv];
		info.day += 1;
	}
	self.currentDay = [self _timelineAtIndex:1].date;
    [self _updateDateLabel];
    [self addSubview:self.daysBackgroundView];
    [self addSubview:self.monthYearLabel];
	NSInteger cnt = 0;    
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];    
    dateFormat.dateFormat = @"eee";    
    dateFormat.timeZone = self.timeZone;    
    NSDateComponents *sund = [[NSDateComponents alloc] init];    
    sund.day = 5;    
    sund.month = 12;    
    sund.year = 2010;    
    sund.hour = sund.minute = sund.second = sund.weekday = 0;    
    sund.timeZone = self.timeZone;    
    NSString * sun = [dateFormat stringFromDate:[NSDate dateWithDateComponents:sund]];    
    sund.day = 6;
    NSString *mon = [dateFormat stringFromDate:[NSDate dateWithDateComponents:sund]];    
    sund.day = 7;
    NSString *tue = [dateFormat stringFromDate:[NSDate dateWithDateComponents:sund]];    
    sund.day = 8;
    NSString *wed = [dateFormat stringFromDate:[NSDate dateWithDateComponents:sund]];    
    sund.day = 9;
    NSString *thu = [dateFormat stringFromDate:[NSDate dateWithDateComponents:sund]];
    sund.day = 10;
    NSString *fri = [dateFormat stringFromDate:[NSDate dateWithDateComponents:sund]];    
    sund.day = 11;
    NSString *sat = [dateFormat stringFromDate:[NSDate dateWithDateComponents:sund]];    
    NSArray *ar;
    if(self.sunday) ar = @[sun,mon,tue,wed,thu,fri,sat];
    else ar = @[mon,tue,wed,thu,fri,sat,sun];    
	NSInteger per = CGFrameGetWidth(self) - (DAY_LABEL_WIDTH *7);
	per /= 7;
	NSInteger minX = per / 2;	
	for(NSString *str in ar){		
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(minX + cnt*(DAY_LABEL_WIDTH+per) , 0, DAY_LABEL_WIDTH, 20)];        
        if ([str isEqualToString:@"Sun"]) {
            label.accessibilityLabel = @"Sunday";
        }
        else if ([str isEqualToString:@"Mon"]) {
            label.accessibilityLabel = @"Monday";
        }
        else if ([str isEqualToString:@"Tue"]) {
            label.accessibilityLabel = @"Tuesday";
        }
        else if ([str isEqualToString:@"Wed"]) {
            label.accessibilityLabel = @"Wednesday";
        }
        else if ([str isEqualToString:@"Thu"]) {
            label.accessibilityLabel = @"Thursday";
        }
        else if ([str isEqualToString:@"Fri"]) {
            label.accessibilityLabel = @"Friday";
        }
        else if ([str isEqualToString:@"Sat"]) {
            label.accessibilityLabel = @"Saturday";
        }
		label.font = [UIFont systemFontOfSize:10];
        label.text = [str substringToIndex:1];		
        label.textColor = cnt == 0 || cnt == 6 ? WEEKEND_TEXT_COLOR : [UIColor blackColor];		
        label.textAlignment = NSTextAlignmentCenter;		
        label.userInteractionEnabled = NO;		
        [self.daysBackgroundView addSubview:label];		
        [label sizeToFitWithAlignment];
		cnt++;
	}	
	[self.daysBackgroundView addSubviewToBack:self.daysScrollView];	
	UIView *dayContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.daysScrollView.contentSize.width, CGRectGetHeight(self.daysScrollView.frame))];    
	[self.daysScrollView addSubview:dayContainerView];	
	CGRect weekFrame = self.daysScrollView.frame;	
    weekFrame.origin = CGPointZero;	
    for(NSInteger i=0;i<3;i++){		
		weekFrame.origin.x = CGRectGetWidth(weekFrame) * i;	
        TKWeekdaysView *weekdayView = [[TKWeekdaysView alloc] initWithFrame:weekFrame];		
		for(UILabel *label in weekdayView.weekdayLabels){		
            label.userInteractionEnabled = YES;			
            [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapWeekdayLabel:)]];
		}		
        [dayContainerView addSubviewToBack:weekdayView];
        [self.weekdayPages addObject:weekdayView];
	}
}

This is to fix the dates :
- (void) _setupDaysView{
	NSArray *labels =  [self _allDayLabels];
    NSDateComponents *comp = [self.calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitEra | NSCalendarUnitWeekday fromDate:self.currentDay];
    NSDateComponents *mutedCom = [self.calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitEra fromDate:self.currentDay];
    for(NSInteger cnt= comp.weekday + 5;cnt>=0;cnt--){
        NSDate *aDate = [self.calendar dateFromComponents:mutedCom];
        mutedCom = [self.calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitEra fromDate:aDate];
        TKDateLabel *label = labels[cnt];
        label.text = [NSString stringWithFormat:@"%@",@(mutedCom.day)];
        label.date = aDate;
        label.today = [aDate isTodayWithTimeZone:self.calendar.timeZone];
        label.selected = [aDate isSameDay:self.currentDay timeZone:self.calendar.timeZone];
        mutedCom.day --;
	}
	mutedCom = [self.calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitEra fromDate:self.currentDay];
	for(NSInteger cnt= comp.weekday + 5;cnt<labels.count;cnt++){
		NSDate *aDate = [self.calendar dateFromComponents:mutedCom];
        mutedCom = [self.calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitEra fromDate:aDate];
        TKDateLabel *label = labels[cnt];
        label.text = [NSString stringWithFormat:@"%@",@(mutedCom.day)];
        label.date = aDate;
        label.today = [aDate isTodayWithTimeZone:self.calendar.timeZone];
        label.selected = [aDate isSameDay:self.currentDay timeZone:self.calendar.timeZone];
        mutedCom.day ++;
	}
	self.indexOfCurrentDay = comp.weekday-1;
}

I jus tried this and it worked out but if possible explain me the logic in the _setupDaysView function.Thank you.

@richardtop
Copy link

@ranjith1109 I've made a library which gets first day of the week from user's current locale automatically:
https://github.com/richardtop/CalendarKit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants