1

I am using JTCalendar. I want to get the month for which calendar in shown. As of now no month name is shown in calendar. I have used below code to show calendar.

  calendarManager = JTCalendarManager()
  calendarManager?.delegate = self
  var calender = calendarManager?.dateHelper.calendar()
  calender?.firstWeekday = 1
  calendarManager?.contentView = calendarContentView
  calendarManager?.setDate(todayDate)

Please guide how can I show month name with year.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
dhiraj
  • 83
  • 2
  • 10

4 Answers4

2

Just set your month's UIView class to "JTCalendarMenuView" and then, you can use the JTCalendarDelegate Methods as:

    - (UIView *)calendarBuildMenuItemView:(JTCalendarManager *)calendar
{
    UILabel *label = [UILabel new];

    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:16];

    return label;
}

- (void)calendar:(JTCalendarManager *)calendar prepareMenuItemView:(UILabel *)menuItemView date:(NSDate *)date
{
    static NSDateFormatter *dateFormatter;
    if(!dateFormatter){
        dateFormatter = [NSDateFormatter new];
        dateFormatter.dateFormat = @"MMMM yyyy";
    }
    menuItemView.text = [dateFormatter stringFromDate:date];
}

This will give you text as "December 2016" (for eg.)

Aakash
  • 2,239
  • 15
  • 23
  • I am not using menu view.I am using contentView only.Please help with that – dhiraj Dec 06 '16 at 12:43
  • You will have to place a view above your content view and then set its class to "JTCalendarMenuView" in order to get the month view. – Aakash Dec 06 '16 at 12:45
2

Swift 4+:

Simple solution

var getMonthYearFromDate: String? 
{
     let dateFormatter = DateFormatter()
     dateFormatter.dateFormat = "MMMM, yyyy"
     let strMonth = dateFormatter.string(from: self)
     return strMonth
}

func calendar(_ calendar: JTCalendarManager!, prepareMenuItemView menuItemView: UIView!, date: Date!) 
{
     print(date.getMonthYearFromDate)

     //Do your code modification here
}
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
1

In addition to @Aakash's answer, menuItemView is actually a UIView. So it does not have .text property. Instead use this line, [(UILabel*) menuItemView setText:[dateFormatter stringFromDate:date]];

Raj D
  • 205
  • 3
  • 16
0

Swift

just add this function.

   func calendar(_ calendar: JTCalendarManager!, prepareMenuItemView menuItemView: UIView!, date: Date!) {
    for view in menuItemView.subviews {
        view.removeFromSuperview()
    }
    let label1 = UILabel(frame: CGRect(x: 80, y: 60, width:400, height: 21))
    label1.center = CGPoint(x: 130, y: 20)
    label1.textColor = UIColor.red
    label1.textAlignment = NSTextAlignment.center
    let dateFormatter1 = DateFormatter()
    dateFormatter1.dateFormat = "MMMM, yyyy";
    let mydt = dateFormatter1.string(from: date)
    label1.text = mydt
    menuItemView.addSubview(label1)
}
Ahtazaz
  • 903
  • 8
  • 21