首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在JTCalendar中获取带有year的月份名称?

如何在JTCalendar中获取带有year的月份名称?
EN

Stack Overflow用户
提问于 2016-12-06 20:20:01
回答 4查看 1.1K关注 0票数 1

我正在使用JTCalendar。我想要获取显示日历的月份。到目前为止,日历中还没有显示月份名称。我使用了下面的代码来显示日历。

代码语言:javascript
复制
  calendarManager = JTCalendarManager()
  calendarManager?.delegate = self
  var calender = calendarManager?.dateHelper.calendar()
  calender?.firstWeekday = 1
  calendarManager?.contentView = calendarContentView
  calendarManager?.setDate(todayDate)

请指导我如何用年份显示月份名称。

EN

回答 4

Stack Overflow用户

发布于 2016-12-06 20:39:11

只需将月份的UIView类设置为"JTCalendarMenuView“,然后就可以像这样使用JTCalendarDelegate方法:

代码语言:javascript
复制
    - (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月”(例如)

票数 2
EN

Stack Overflow用户

发布于 2017-03-24 13:26:56

除了@Aakash的回答之外,menuItemView实际上是一个UIView。所以它没有.text属性。改为使用此行,[(UILabel*) menuItemView setText:dateFormatter :date];

票数 1
EN

Stack Overflow用户

发布于 2019-02-13 16:44:02

Swift 4+:

简单解决方案

代码语言:javascript
复制
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
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40995282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档