0

I have a project using FSCalendar in swift 4. I have events as green and red color. But while selecting the particular dates , the color of events changes to selection color. How can I solve this issue as given in image below. The event dots on the blue selection color must come either green or red.

For image you can see this link : https://github.com/WenchaoD/FSCalendar/issues/919

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Swift guy
  • 25
  • 2
  • 8

2 Answers2

4

I think you have not set the eventSelectionColor Property of FSCalendar.

You can set it programmatically by using below code.

calendar.appearance.eventSelectionColor = UIColor.green

or

You can set it from storyboard in Attributes inspector field.Set the event selection color property of FSCalendar as shown in below image.

enter image description here

Hope this will help you.

Khushbu
  • 2,342
  • 15
  • 18
  • the color of the events can be single dot with red or green color , 2 dots with green and red color and may be no dots. The answer you gave will be only possible of same color of dots. – Swift guy Apr 25 '18 at 12:03
  • but if there is a event, then the dot will show. So, your question of no dots is solved. Now, you have asked for dots. so, you have to check your condition when you have to show which color of dot. – Khushbu Apr 25 '18 at 12:09
  • No, after selection of dates, the events color changes to selection color. I need the color of events not to be changed. – Swift guy Apr 25 '18 at 12:12
  • yes for that as I told you, you have to set the "eventSelectionColor" property of FSCalendar. Till now you have only set the "eventDefaultColor" property of FSCalendar. So, when you select the date the color will change. – Khushbu Apr 25 '18 at 12:16
  • Yes, I know to set one color for "eventSelectionColor". But how to set "eventSelectionColor" for 2 colors and no color. @ Khushbu – Swift guy Apr 26 '18 at 04:51
  • @Swiftguy, In question you have asked that you don't know how to set the event color of selected dates. So, i have give the answer of that. – Khushbu Apr 26 '18 at 05:10
  • For set the event color of selected dates you can write the code where you have set the color for "eventDefaultColor" property. – Khushbu Apr 26 '18 at 05:12
  • I have set "eventDefaultColor" property from the attributes inspector. But it allows only one color. – Swift guy Apr 26 '18 at 05:19
  • @Swiftguy I am not able to understand you. I am not getting what you want exactly. – Khushbu Apr 26 '18 at 05:37
  • https://github.com/WenchaoD/FSCalendar/issues/919... go through this link.It will help to understand – Swift guy Apr 26 '18 at 06:14
0

For multiple event with different colors, you have to use FSCalendarDelegateAppearance method.

Example::::

//MARK: - set formatter of date

fileprivate let formatter: DateFormatter = {

        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter }()

//MARK: - assign array of event

var arrayOfEvent1 : [String] = ["2018-08-14","2018-08-15"]

var arrayOfEvent2 : [String] = ["2018-08-14","2018-09-16"]



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

     let strDate = self.formatter.string(from:date)

         if arrayOfEvent1.contains(strDate) && arrayOfEvent2.contains(strDate) 
         {
             return 2
         }
         else if arrayOfEvent1.contains(strDate)
         {
             return 1
         }
         else if arrayOfEvent2.contains(strDate)
         {
             return 1
         }

     return 0
}


func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance,eventDefaultColorsFor date: Date) -> [UIColor]?
{


    let strDate = formatter.string(from: date)

    if arrayOfEvent1.contains(strDate) && arrayOfEvent2.contains(strDate)
    {
        return [UIColor.red ,UIColor.blue]
    }
    else if arrayOfEvent1.contains(strDate)
    {
        return [UIColor.red]
    }
    else if arrayOfEvent2.contains(strDate)
    {
        return [UIColor.blue]
    }

    return [UIColor.clear]

}

func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventSelectionColorsFor date: Date) -> [UIColor]? {
      let strDate = formatter.string(from: date)

        if arrayOfEvent1.contains(strDate) && arrayOfEvent2.contains(strDate)                   
        {
            return [UIColor.red ,UIColor.blue]
        }
        else if arrayOfEvent1.contains(strDate)
        {
             return [UIColor.red]
        }
        else if arrayOfEvent2.contains(strDate)
        {
             return [UIColor.blue]
        }

            return [UIColor.clear]
    }

After execution of above delegate method we get two events on "2018-08-14".first event color is red and second event color is blue.

Hope this will help you.

Yash Bhikadiya
  • 188
  • 1
  • 9