FSCalendar icon indicating copy to clipboard operation
FSCalendar copied to clipboard

Change weekday label color if day is a weekend

Open osorochich opened this issue 7 years ago • 13 comments

Hi there,

First of all thanks a lot for the great calendar implementation!

BTW I have a question: is it possible to set different colors for weekday labels? According to our style guide I have to set one color for weekday which represents a working day and another one for a day off. In current API I can see only the property weekdayTextColor but it changes color for all weekdays label and unfortunately it doesn't fit my requirements.

So, is it possible to manage the situation I've described above using existing API?

Thanks in advance!

osorochich avatar Dec 21 '16 17:12 osorochich

Hi,

You can implement the method titleDefaultColorFor from the FSCalendarDelegateAppearance delegate and verify if the date is a dayOff or not. For example:

    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
        if date == dayOff {
            return UIColor.dayOffColor
        } else {
            return UIColor.notDayOffColor
        }
    }

kallahir avatar Dec 21 '16 19:12 kallahir

Hi @kallahir,

Thanks for your response! Actually, I have to change color of weekday labels (e.g. Monday, Tuesday..etc) at the top of calendar. The delegate method you've attached is used to configure a title color for a particular date and it is not that I'm looking for.

osorochich avatar Dec 21 '16 19:12 osorochich

Actually, I have a tricky way to do so, but I don't like such solution and I'd like to use some public declared API. This is the solution I use for the moment:

        let weekdayEnumerator = calendar.calendarWeekdayView.weekdayLabels.objectEnumerator()
        var index = 0
        while let label = weekdayEnumerator.nextObject() as? UILabel {
            if index == 0 || index == 6 {
                label.textColor = UIColor.dayOffColor()
            } else {
                label.textColor = UIColor.notDayOffColor()
            }
            
            index += 1
        }

osorochich avatar Dec 21 '16 19:12 osorochich

Hmm.. I misunderstood what you said. For this scenario I don't know any public API method, but your solution seems quite good. I'll take a look later and see if I find anything that might help you.

kallahir avatar Dec 21 '16 19:12 kallahir

Unfortunately, this solution works well except the case when reloadData message is sent to FSCalendar instance, because under the hood it calls [self invalidateWeekdayTextColor]; and it resets all colors to default. I'll be happy if you'll find a better way to do so. Thanks in advance!

osorochich avatar Dec 21 '16 20:12 osorochich

Hi, I spent some time looking at it and you right about it. A property for weekendDayTextColor must be created in order for you to be able to customize it as you want. I wasn't able to find a solution, but If in the near future I have some spare time I'll try to solve this. If you come up with something please let me know.

kallahir avatar Dec 28 '16 03:12 kallahir

@osorochich, I require the same thing. Before (in v2.4.0), I was able to customize the weekend day color (i.e., Sun and Sat headers) by doing the following:

(calendar.weekdays[0] as! UILabel).textColor = UIColor.red
(calendar.weekdays[6] as! UILabel).textColor = UIColor.red

But with the latest implementation, it is no longer possible.

Thanks for submitting this.

h-bomb avatar Jan 09 '17 17:01 h-bomb

A workaround is to get labels by calendar.calendarWeekdayView.weekdayLabels.

WenchaoD avatar Jan 11 '17 02:01 WenchaoD

@WenchaoD: I tried this before commenting above, but on viewDidLoad, all of the days are not set:

screen shot 2017-01-10 at 7 12 21 pm

h-bomb avatar Jan 11 '17 03:01 h-bomb

@h-bomb I tried some asynchronous method and concurrent enumeration to optimize the user's experience, but it seems made a conflict with direct-access to calendarWeekdayView (and maybe calendarHeaderView). Is there a better idea?

WenchaoD avatar Jan 12 '17 17:01 WenchaoD

@WenchaoD: Rather than providing access to the day of week headers, it would be great if you could provide another setting that would allow us to customize the color of the weekend headers, since we are able to customize the weekend day numbers (i.e. calendar.appearance.titleWeekendColor (from v2.4.0)). Example:

screen shot 2017-01-12 at 10 14 26 am Note the Sun and Sat headers.

Thanks.

h-bomb avatar Jan 12 '17 18:01 h-bomb

screen shot 2018-11-30 at 6 23 21 pm If you want to change color of weekDay like in image you can do like this.

let weekDayLabel = fsCalendarInstance.calendarWeekdayView.weekdayLabels for weekDay in weekDayLabel { weekDay.textColor = UIColor.yourFavoriteColor }

amit68029 avatar Nov 30 '18 12:11 amit68029

Update to h-bomb's version:

        calendar.calendarWeekdayView.weekdayLabels[0].textColor = UIColor.red
        calendar.calendarWeekdayView.weekdayLabels[6].textColor = UIColor.red

mostafaelshazly9 avatar Nov 15 '21 10:11 mostafaelshazly9