1

我正在使用 MBCalendarKit 来自定义日历。我想在每个日历单元格中添加文本标签。如需参考,请从 github“ https://github.com/MosheBerman/MBCalendarKit ”下载示例代码。

编辑:

为了制作多个标签,我使用了 for 循环。怎么了?

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;
4

1 回答 1

0

如果您使用的是旧版本的 MBCalendarKit,您可能必须直接修改 CKCalendarCell 类,以在单元格内显示自定义内容。

MBCalendarKit 5 进行了一些更改,允许向单元格添加自定义内容。CKCustomCellProvider在您自己的代码中实现协议。您需要实现两个部分:

  1. customCellClass 方法。
  2. calendarView:willDisplayCell:inContext: 方法。

如果您想用自己的单元格内容替换原始单元格内容,那么您应该创建一个自定义 UICollectionView 子类并从 customCellClass 返回该类。在 calendarView:willDisplayCell:inContext: 中,您将使用自动布局约束设置您的单元格。

如果要修改默认单元格的内容,可以CKCalendarCell.class从 customCellClass 返回,然后 calendarView:willDisplayCell:inContext: 将提供默认单元格的实例供您随意修改。

免责声明:我是 MBCalendarKit 的作者

于 2017-08-20T13:12:51.913 回答