Search code examples
swiftswift4.1

How we can change current Month all Past Dates title Color in FSCalendar in Swift


I want to change all past dates title Color in fscalendar in Swift Not A Particular Date I added this bit of code:- Delegate method:-

func calendar(calendar: FSCalendar!, appearance: FSCalendarAppearance!, titleDefaultColorForDate date: NSDate!) -> UIColor! {
    
    if date .compare(Date()) == .orderedAscending {
        return .green
    }else {
        return .red
    }
}

Solution

  • Try to connect delegate and data source of calendar view and check because I got the output with this method

    class YourViewController: UIViewController, FSCalendarDelegate, FSCalendarDataSource, FSCalendarDelegateAppearance {
                
                 .....
                
         override func viewDidLoad()
         {
              super.viewDidLoad()
              yourCalendarView.delegate = self
              yourCalendarView.dataSource = self
            
              //If you are using storyboard make sure data source and delegates connected
                       
         }
            
                   ......
            
         func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
            
             //Remove timeStamp from date
             if date.removeTimeStamp!.compare(Date().removeTimeStamp!) == .orderedAscending {
            
                return .green
            
             }else if date.removeTimeStamp!.compare(Date().removeTimeStamp!) == .orderedDescending{
            
                return .red
            
             } else {
            
                return .black
    
             }
                    
          }
    }
    
    extension Date {
    
        public var removeTimeStamp : Date? {
            guard let date = Calendar.current.date(from: Calendar.current.dateComponents([.year, .month, .day], from: self)) else {
             return nil
            }
            return date
        }
    }
    

    Output:

    enter image description here

    And if you want same title color for past and future dates, Use below code

    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
                
         if date.removeTimeStamp!.compare(Date().removeTimeStamp!) == .orderedAscending || date.removeTimeStamp!.compare(Date().removeTimeStamp!) == .orderedDescending {
                    
             return .green
                    
          } else {
                    
             return .black
          }
                
    }
    

    Output:

    enter image description here