I'm using FSCalendar in my iOS app and I want to customize the background color of weekdays (Monday to Friday) along with highlighting the selected date. I have attached an image to show the desired effect.
if let weekdayView = calendar.calendarWeekdayView {
for label in weekdayView.weekdayLabels {
label.backgroundColor = UIColor.lightGray.withAlphaComponent(0.2) // Change weekday background color
label.textColor = .black
label.layer.cornerRadius = 10
label.layer.masksToBounds = true
}
}
calendar.appearance.selectionColor = UIColor.orange
This changes the weekday text color but does not fully apply a background color to the entire date.
Question:- How to apply a background color to FSCalendar weekdays while keeping the selected date highlighted?
Following image is my original image. Screen Shot
I want to this:- Screen Shot
I'm using FSCalendar in my iOS app and I want to customize the background color of weekdays (Monday to Friday) along with highlighting the selected date. I have attached an image to show the desired effect.
if let weekdayView = calendar.calendarWeekdayView {
for label in weekdayView.weekdayLabels {
label.backgroundColor = UIColor.lightGray.withAlphaComponent(0.2) // Change weekday background color
label.textColor = .black
label.layer.cornerRadius = 10
label.layer.masksToBounds = true
}
}
calendar.appearance.selectionColor = UIColor.orange
This changes the weekday text color but does not fully apply a background color to the entire date.
Question:- How to apply a background color to FSCalendar weekdays while keeping the selected date highlighted?
Following image is my original image. Screen Shot
I want to this:- Screen Shot
Share Improve this question edited Mar 13 at 13:23 Joakim Danielson 52.3k5 gold badges33 silver badges71 bronze badges asked Mar 13 at 12:39 Sham DhimanSham Dhiman 1,5661 gold badge28 silver badges69 bronze badges 2- Isn't this the same question that you've asked 5 years ago? How to change background color in FSCalendar customise Swift – David Pasztor Commented Mar 13 at 14:49
- @DavidPasztor, Both are different. – Sham Dhiman Commented Mar 13 at 14:58
1 Answer
Reset to default 0import UIKit
import FSCalendar
class ViewController: UIViewController, FSCalendarDelegate, FSCalendarDataSource, FSCalendarDelegateAppearance {
@IBOutlet weak var calendar: FSCalendar!
override func viewDidLoad() {
super.viewDidLoad()
calendar.delegate = self
calendar.dataSource = self
calendar.appearance.selectionColor = UIColor.orange // Selected date color
// Customize weekday labels
if let weekdayView = calendar.calendarWeekdayView {
for label in weekdayView.weekdayLabels {
label.backgroundColor = UIColor.lightGray.withAlphaComponent(0.2) // Background for weekdays
label.textColor = .black
label.layer.cornerRadius = 10
label.layer.masksToBounds = true
}
}
}
// MARK: - FSCalendarDelegateAppearance
func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, fillDefaultColorFor date: Date) -> UIColor? {
let calendar = Calendar.current
let weekday = calendarponent(.weekday, from: date) // Get weekday number
if weekday >= 2 && weekday <= 6 { // Monday to Friday (Sunday = 1)
return UIColor.lightGray.withAlphaComponent(0.2) // Background color for weekdays
}
return nil
}
func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, fillSelectionColorFor date: Date) -> UIColor? {
return UIColor.orange // Selected date highlight
}
}