最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - Change the background color of weekdays along with the selected date in FSCalendar? - Stack Overflow

programmeradmin8浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0
import 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
    }
}
发布评论

评论列表(0)

  1. 暂无评论