-1

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
    }
}

1 Answers1

2

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

Kishan Bhatiya
  • 2,175
  • 8
  • 14