Dheeraj Chahar
Dheeraj Chahar

Reputation: 116

How to add label in calendar cell content in MBCalendarKit?

I'm using MBCalendarKit for customising calendar. I want to add to text labels in each calendar cell. For reference download sample code from github "https://github.com/MosheBerman/MBCalendarKit".

Edit:

To make multiple labels I used a for loop. What's wrong?

NSMutableArray *dateButtons = [NSMutableArray array];
    for (NSInteger i = 1; i <= 42; i++) {
        DateButton *dateButton = [DateButton buttonWithType:UIButtonTypeCustom];
        dateButton.calendar = self.calendar;
        [dateButton addTarget:self action:@selector(_dateButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

        UILabel *travelTime = [[UILabel alloc] initWithFrame:CGRectMake(3, 23, 20, 20)];
        travelTime.font=[travelTime.font fontWithSize:9];
        [travelTime setTextColor:[UIColor blueColor]];
        travelTime.text = @"9";

        UILabel *workTime = [[UILabel alloc] initWithFrame:CGRectMake(30, 23, 20, 20)];
        workTime.font=[workTime.font fontWithSize:9];
        [workTime setTextColor:[UIColor orangeColor]];
        workTime.text = @"9";

        [dateButton addSubview:travelTime];
        [dateButton addSubview:workTime];
        [dateButtons addObject:dateButton];
    }
self.dateButtons = dateButtons;

Upvotes: 1

Views: 103

Answers (1)

Moshe
Moshe

Reputation: 58087

If you’re using an older version of MBCalendarKit, you’d probably have to modify the CKCalendarCell class directly, to show custom content inside of cells.

MBCalendarKit 5 has some changes which allow for adding custom content to cells. Implement the CKCustomCellProvider protocol in your own code. There are two parts that you need to implement:

  1. The customCellClass method.
  2. The calendarView:willDisplayCell:inContext: method.

If you want to replace the original cell content with your own, then for you should create a custom UICollectionView subclass and return that class from customCellClass. Inside calendarView:willDisplayCell:inContext: you would set up your cell with autolayout constraints.

If you want to modify the contents of the default cell, you can return CKCalendarCell.class from customCellClass, and then calendarView:willDisplayCell:inContext: will vend instances of the default cell for you to modify as you wish.

Disclaimer: I’m the author of MBCalendarKit

Upvotes: 0

Related Questions