FSCalendar
FSCalendar copied to clipboard
Change weekday label color if day is a weekend
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!
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
}
}
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.
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
}
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.
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!
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.
@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.
A workaround is to get labels by calendar.calendarWeekdayView.weekdayLabels
.
@WenchaoD: I tried this before commenting above, but on viewDidLoad
, all of the days are not set:
@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: 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:
Note the Sun and Sat headers.
Thanks.
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 }
Update to h-bomb's version:
calendar.calendarWeekdayView.weekdayLabels[0].textColor = UIColor.red
calendar.calendarWeekdayView.weekdayLabels[6].textColor = UIColor.red