Ios 如何在FSCalendar中获取月份

Ios 如何在FSCalendar中获取月份,ios,swift,fscalendar,Ios,Swift,Fscalendar,我正在使用pod FSCalendar(),为了对其进行本地化,我使用了: calendar.locale = Locale(identifier: String) 但问题是,翻译月份的最后几个字母在语法上听起来不太好。(通缉令中的“2016年12月”与“2016年12月”类似) 在故事板上,我只有UIView,你们能帮我理解这个pod中这个月的确切答案吗? 据我所知,我可以这样做 if monthLabel.text.hasPrefix("") { monthLabel.text =

我正在使用pod FSCalendar(),为了对其进行本地化,我使用了:

 calendar.locale = Locale(identifier: String) 
但问题是,翻译月份的最后几个字母在语法上听起来不太好。(通缉令中的“2016年12月”与“2016年12月”类似) 在故事板上,我只有UIView,你们能帮我理解这个pod中这个月的确切答案吗? 据我所知,我可以这样做

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

}

是的,FSCalendar的实现是错误的——这就像使用locale.calendar.monthSymbols而不是locale.calendar.standalonemonsymbols一样

快速解决方法是修复文件FSCalendarHeaderView.m中的FSCalendar代码

(void)configureCell:(FSCalendarHeaderCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    case FSCalendarScopeMonth: {
        //...
        //...
        text = [_calendar.formatter stringFromDate:date]; //**1
    }
}
**我必须换成像这样的东西

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

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

是的,FSCalendar的实现是错误的——这就像使用locale.calendar.monthSymbols而不是locale.calendar.standalonemonsymbols一样

快速解决方法是修复文件FSCalendarHeaderView.m中的FSCalendar代码

(void)configureCell:(FSCalendarHeaderCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    case FSCalendarScopeMonth: {
        //...
        //...
        text = [_calendar.formatter stringFromDate:date]; //**1
    }
}
**我必须换成像这样的东西

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

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

对于FSCalendarHeaderView.m更改函数中的俄语区域设置

  • (void)configureCell:(FSCalendarHeaderCell*)单元格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];
}

对于FSCalendarHeaderView.m更改函数中的俄语区域设置

  • (void)configureCell:(FSCalendarHeaderCell*)单元格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];
}

Swift 3中的快速解决方案

首先继承FSCalendarDelegate,然后添加此

func calendarCurrentPageDidChange(_ calendar: FSCalendar) {

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

此“当前月”将在您每次滚动每月日历时更改。

Swift 3中的快速解决方案

首先继承FSCalendarDelegate,然后添加此

func calendarCurrentPageDidChange(_ calendar: FSCalendar) {

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

此“currentMonth”将在您每个月滚动日历时更改。

您可以使用以下代码轻松轻松地完成此操作:

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

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

要了解有关日期格式设置的更多信息,请使用

中的表格,您可以使用以下代码轻松轻松地完成此操作:

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

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

要了解有关日期格式设置的更多信息,请使用表中的内容

这是在俄语-works perfectHello上本地化的唯一方法这是在俄语-works perfectHello上本地化的唯一方法,以帮助其他人理解您的答案,考虑一下代码的简短描述以及它如何解决所发布的问题。为什么要使用NSLaLaLE,然后将其转换为区域设置,而不是直接使用区域设置?这样做真的没有意义同样,正如亨利所说,你的答案如何解决OP的问题还不清楚。你好,帮助其他人理解你的答案,考虑包括你的代码的简短描述以及它如何解决所发布的问题。为什么你要使用NSLaLaLE,然后将它作为区域设置,而不是直接使用区域设置?这样做真的没有意义此外,正如Henry所说,不清楚您的答案如何解决OP的问题。类型“FSCalendar”的值没有成员“month”,您是如何得到解决方案的?类型“FSCalendar”的值没有成员“month”,您是如何得到解决方案的?