标记Hijri日历的日期

时间:2013-08-13 22:09:20

标签: ios objective-c cocoa-touch nscalendar hijri

我是iOS开发的新手(来自Android开发),我需要一些帮助。我正在使用Vurig's library来显示自定义日历控件,并在日历中标记多个日期。

问题是我想标记Hijri日期而不是格里高利。我怎么能这样做?

到目前为止,我已将Hijri日期转换为格里高利,以在日历上标记日期,但它仅标记一个日期,而我想在日历月中标记多个日期。

到目前为止的代码:

-(void)calendarView:(VRGCalendarView *)calendarView switchedToMonth:(int)month targetHeight:(float)targetHeight animated:(BOOL)animated {
    NSArray *dates;


    NSDate *date = [self addDates:calendarView andDay:6 andMonth:10];

//    if (month==8){
//       dates = [NSArray arrayWithObjects:[NSNumber numberWithInt:5],[NSNumber numberWithInt:25], nil];
//    }
//    if (month==9) {
//       dates = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:5], nil];
//       
//    }

    //if (month==[[NSDate date] month]) {

    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:( NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSWeekCalendarUnit) fromDate:date];

    int gmonth = [dateComponents month];
    if (month == gmonth) {
        dates = [NSArray arrayWithObjects:date, nil];
    }

    [calendarView markDates:dates];


}

-(void)calendarView:(VRGCalendarView *)calendarView dateSelected:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; [dateFormatter setDateFormat:@"dd MMMM"];

    NSLog(@"Selected date = %@",[dateFormatter stringFromDate:date]);

    //NSLog(@"Selected date = %@",date);
}


-(NSDate *)addDates:(VRGCalendarView *)calendarView andDay:(int)day andMonth:(int)month{
    // Then create an Islamic calendar
    NSCalendar *hijriCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];

    // And grab those date components for the same date
    NSDateComponents *hijriComponents = [[NSDateComponents alloc] init];

    hijriComponents.day = day;
    hijriComponents.month = month;
    hijriComponents.year = 1434;

    NSDate *hdate = [hijriCalendar dateFromComponents:hijriComponents];


    // Create a Gregorian Calendar
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    // Set up components of a Gregorian date
    NSDateComponents *gregorianComponents = [gregorianCalendar components:(NSDayCalendarUnit |
                                                                           NSMonthCalendarUnit |
                                                                           NSYearCalendarUnit)
                                                                 fromDate:hdate];
    // Create the date
    NSDate *date = [gregorianCalendar dateFromComponents:gregorianComponents];

    NSLog(@"[In Hijri calendar ->] Date is %@", date);
    return date;

}

1 个答案:

答案 0 :(得分:1)

您需要做的就是将NSDate的数组传递给markDates:方法。您不需要首先转换为格里高利,因为NSDate只是一个时间戳,并且与日历,时区等无关。

我下载了VURIG日历示例项目,并在VRGViewController中使用以下内容替换了calendarView:switchedToMonth:targetHeight:animated:方法,该方法应准确说明您的需求:

-(void)calendarView:(VRGCalendarView *)calendarView switchedToMonth:(int)month targetHeight:(float)targetHeight animated:(BOOL)animated {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:6];
    [components setMonth:10];
    [components setYear:1434];
    NSDate *date1 = [calendar dateFromComponents:components];

    [components setDay:10];
    NSDate *date2 = [calendar dateFromComponents:components];

    [calendarView markDates:@[date1,date2]];
}

这标志着日历上的8月12日和8月16日,虽然我不熟悉Hijri日历,但我可以说它是准确的。