我正在使用JTCalendar。我想知道显示哪个日历的月份。截至目前,日历中没有显示月份名称。我使用下面的代码来显示日历。
calendarManager = JTCalendarManager()
calendarManager?.delegate = self
var calender = calendarManager?.dateHelper.calendar()
calender?.firstWeekday = 1
calendarManager?.contentView = calendarContentView
calendarManager?.setDate(todayDate)
请指导我如何用年份显示月份名称。
答案 0 :(得分:2)
只需将月份的UIView类设置为“JTCalendarMenuView”,然后就可以使用JTCalendarDelegate方法:
- (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];
}
这将为您提供“2016年12月”(例如)
的文字答案 1 :(得分:1)
除了@ Aakash的回答,menuItemView实际上是一个UIView。所以它没有.text属性。而是使用这条线, [(UILabel *)menuItemView setText:[dateFormatter stringFromDate:date]];
答案 2 :(得分:0)
快速
只需添加此功能。
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)
}
答案 3 :(得分:0)
快速4 +:
简单的解决方案
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
}