Search code examples
swiftfscalendar

FSCalendar - Change the title color of Particular Dates Using Swift


Hello all I'm working on the WenchaoD's FSCalendar now a days.I successfully loaded the calendar with many events.But now the question is how to change the date's title color for particular dates.Can anyone suggest me how to do this?


Solution

  • 1) First of all implement FSCalendarDelegateAppearance

    2) Let's assume you are having an array of some dates,let's declare an array first.

    var somedays : Array = [String]()
    

    3) Now you will need formatter to change the string into date.

    fileprivate let gregorian: Calendar = Calendar(identifier: .gregorian)
    fileprivate lazy var dateFormatter1: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter
    }()
    

    4) FSCalendarDelegateAppearance is having one method named : titleDefaultColorFor

    5) Implement this method using below code.

    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
    
        somedays = ["2017-06-03",
                    "2017-06-06",
                    "2017-06-12",
                    "2017-06-25"]
        let dateString : String = dateFormatter1.string(from:date)
    
        if self.somedays.contains(dateString) {
            return .green
        } else {
            return nil
        }
    }
    

    6) Run this code.Happy coding.