如何在FSCalendar中获得月份

时间:2016-12-07 16:17:20

标签: ios swift fscalendar

我正在使用pod FSCalendar(https://github.com/WenchaoD/FSCalendar)并对其进行本地化我使用:

 calendar.locale = Locale(identifier: String) 

但问题是翻译月份的最后几个字母在语法上听起来不太好。(就像“2016年12月”而不仅仅是“想要的语言中的2016年12月”) 在故事板上我只有UIView,请你帮忙了解这个pod中月份的确切答案? 据我所知,我可以像这样做。

 if monthLabel.text.hasPrefix("") {
 monthLabel.text = //change to normal

}

5 个答案:

答案 0 :(得分:2)

Swift 3中的快速解决方案

首先继承FSCalendarDelegate,然后添加此...

func calendarCurrentPageDidChange(_ calendar: FSCalendar) {

    let currentMonth = calendar.month(of: calendar.currentPage)
    print("this is the current Month \(currentMonth)")
}

这" currentMonth"每次每月滚动日历时都会改变。

答案 1 :(得分:1)

是的,FSCalendar有错误的实现 - 它是......比如使用locale.calendar.monthSymbols而不是locale.calendar.standaloneMonthSymbols

快速的灵魂就是将FSCalendar代码修复到文件FSCalendarHeaderView.m

(void)configureCell:(FSCalendarHeaderCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    case FSCalendarScopeMonth: {
        //...
        //...
        text = [_calendar.formatter stringFromDate:date]; //**1
    }
}

** 1必须改为......喜欢

NSDateComponents *cc = [self.calendar.gregorian components:(NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:date];

text = [NSString stringWithFormat:@"%@ %ld", _calendar.formatter.standaloneMonthSymbols[cc.month - 1], (long)cc.year];

答案 2 :(得分:1)

对于FSCalendarHeaderView.m中的俄语语言环境更改功能

  • (void)configureCell:(FSCalendarHeaderCell *)cell atIndexPath:(NSIndexPath *)indexPath

这个

- (void)configureCell:(FSCalendarHeaderCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    FSCalendarAppearance *appearance = self.calendar.appearance;
    cell.titleLabel.font = appearance.headerTitleFont;
    cell.titleLabel.textColor = appearance.headerTitleColor;
    _calendar.formatter.dateFormat = appearance.headerDateFormat;
    BOOL usesUpperCase = (appearance.caseOptions & 15) == FSCalendarCaseOptionsHeaderUsesUpperCase;
    NSString *text = nil;
    switch (self.calendar.transitionCoordinator.representingScope) {
        case FSCalendarScopeMonth: {
            if (_scrollDirection == UICollectionViewScrollDirectionHorizontal) {
                // 多出的两项需要制空
                if ((indexPath.item == 0 || indexPath.item == [self.collectionView numberOfItemsInSection:0] - 1)) {
                    text = nil;
                } else {
                    NSDate *date = [self.calendar.gregorian dateByAddingUnit:NSCalendarUnitMonth value:indexPath.item-1 toDate:self.calendar.minimumDate options:0];
                    text = [_calendar.formatter stringFromDate:date];
                }
            } else {
                NSDate *date = [self.calendar.gregorian dateByAddingUnit:NSCalendarUnitMonth value:indexPath.item toDate:self.calendar.minimumDate options:0];
                text = [_calendar.formatter stringFromDate:date];
            }
            break;
        }
        case FSCalendarScopeWeek: {
            if ((indexPath.item == 0 || indexPath.item == [self.collectionView numberOfItemsInSection:0] - 1)) {
                text = nil;
            } else {
                NSDate *firstPage = [self.calendar.gregorian fs_middleDayOfWeek:self.calendar.minimumDate];
                NSDate *date = [self.calendar.gregorian dateByAddingUnit:NSCalendarUnitWeekOfYear value:indexPath.item-1 toDate:firstPage options:0];
                text = [_calendar.formatter stringFromDate:date];
            }
            break;
        }
        default: {
            break;
        }
    }

    NSArray* foo = [text componentsSeparatedByString: @" "];
    NSString* firstBit = [foo objectAtIndex: 0];

    if ([firstBit isEqualToString:@"января"]) {
        firstBit = @"январь ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"февраля"]) {
        firstBit = @"февраль ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"марта"]) {
        firstBit = @"март ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"апреля"]) {
        firstBit = @"апрель ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"мая"]) {
        firstBit = @"май ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"июня"]) {
        firstBit = @"июнь ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"июля"]) {
        firstBit = @"июль ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"августа"]) {
        firstBit = @"август ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"сентября"]) {
        firstBit = @"сентябрь ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"октября"]) {
        firstBit = @"октябрь ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"ноября"]) {
        firstBit = @"ноябрь ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"декабря"]) {
        firstBit = @"декабрь ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }

    text = usesUpperCase ? text.uppercaseString : text;
    cell.titleLabel.text = text;
    [cell setNeedsLayout];
}

答案 3 :(得分:0)

您可以使用以下代码轻松,完美地完成此任务:

//objc
self.calendar.appearence.headerDateFormat = @"LLLL yyyy";

//swift
self.calendar.appearence.headerDateFormat = "LLLL yyyy"

要详细了解日期格式,请使用 http://userguide.icu-project.org/formatparse/datetime

中的表格

答案 4 :(得分:-1)

var locale = NSLocale(localeIdentifier: "ru_RU")
calendar.locale = locale as Locale