Fscalendar: How to capitalize the first letter of month's name?

Created on 18 Feb 2019  ·  5Comments  ·  Source: WenchaoD/FSCalendar

How to capitalize the first letter of month's name?
I would like to do as in the photo, but when I set the Locale to "pt_BR" it only gets the lowercase month's name.
Sorry if I didn't explain it well.

image

Most helpful comment

@Husseinhj
works perfectly, thanks you.

All 5 comments

@DaniloJrC

By this trick you can do this:

override func viewDidLoad() {
    super.viewDidLoad()

    ...

    self.calendar.locale = Locale.init(identifier: "pt_BR")
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    changeMonthName()
}

func changeMonthName(){
    let collectionView = self.calendar.calendarHeaderView.value(forKey: "collectionView") as! UICollectionView

    collectionView.visibleCells.forEach { (cell) in
    let c = cell as! FSCalendarHeaderCell

    c.titleLabel.text = c.titleLabel.text?.capitalizingFirstLetter()
    }
}

// MARK:- FSCalendarDelegate

func calendarCurrentPageDidChange(_ calendar: FSCalendar) {
    changeMonthName()
}

...

// Out of your class add String extension class
extension String {
    func capitalizingFirstLetter() -> String {
        let first = String(characters.prefix(1)).capitalized
        let other = String(characters.dropFirst())
        return first + other
    }
}

@Husseinhj
works perfectly, thanks you.

@Husseinhj Its not work on v.2.8.1: collectionView.visibleCells is always 0.

@RareScrap

You should call visibleCells method when the calendar has finished creating month views.

Apple documentation:

An array of UICollectionViewCell objects. If no cells are visible, this method returns an empty array.

This issue can help you out.

@Husseinhj

I think I figured out why I had such a problem. The fact is that with pagging disabled, the calendar does not have such a view as calendarHeaderView. You should use visibleStickyHeaders instead:

// will work with disabled pagging
func changeMonthName(){
    for header in calendarView.visibleStickyHeaders {
    let h = header as! FSCalendarStickyHeader
    h.titleLabel.text = h.titleLabel.text?.capitalizingFirstLetter()
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

TomWang1 picture TomWang1  ·  5Comments

adavan2107 picture adavan2107  ·  5Comments

denikaev picture denikaev  ·  5Comments

bizibizi picture bizibizi  ·  3Comments

garudaonekh picture garudaonekh  ·  4Comments