swift3fscalendar

How to change Event Dot Color in FSCalender


Here is my code. I am trying to change the dot color but I did not find any solution. Thanks

func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {


    return 1;
}
func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventColorFor date: Date) -> UIColor? {

        return UIColor.red

}

Here is an example image of these event-dots:

Image of the dots in the calender


Solution

  • This example is taken from here. Basically you just use the given method, check for event-type or something like that and return a color of your favour.

    //Used by one of the example methods
    var datesWithEvent = ["2015-10-03", "2015-10-06", "2015-10-12", "2015-10-25"]
    var datesWithMultipleEvents = ["2015-10-08", "2015-10-16", "2015-10-20", "2015-10-28"]
    
    //Used in one of the example methods
    fileprivate lazy var dateFormatter2: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter
    }()
    

    The complete example is to long for embedding, so i only took 2 example methods. I added the fields from the example, to have an "complete" example of how such a method could look like.

    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventColorFor date: Date) -> UIColor? {
         //Do some checks and return whatever color you want to.
         return UIColor.purple  
    }
    
    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventDefaultColorsFor date: Date) -> [UIColor]? {
        let key = self.dateFormatter2.string(from: date)
        if self.datesWithMultipleEvents.contains(key) {
            return [UIColor.magenta, appearance.eventDefaultColor, UIColor.black]
        }
            return nil
     }
    

    For better understanding have a look at linked example class in Github. That example is pretty self explanatory.