CVCalendar:如何修改日期圆圈背景及文本颜色?
Hey there! I’ve tinkered with CVCalendar extensively for a few projects, so I can help you sort out those color customization headaches you’re hitting. Let’s break down each problem and how to fix them:
1. Changing the red circle background of the current date
CVCalendar’s default present day circle color is controlled via the CVCalendarAppearance object. You might have missed this specific property in the docs—here’s how to set it:
// Initialize or access your existing CVCalendarAppearance instance let calendarAppearance = CVCalendarAppearance() // Replace with your desired color for the current day's circle calendarAppearance.presentDayCircleColor = UIColor.systemGreen // Apply the appearance to your calendar view yourCalendarView.calendarAppearance = calendarAppearance
If this doesn’t take effect immediately, double-check that you aren’t overriding this setting later in your code (e.g., in delegate methods for custom day views).
2. Fixing present day text turning red after selecting a date
The text color of the present day (even when another date is selected) is managed by the presentDayTextColor property. Update it like so:
calendarAppearance.presentDayTextColor = UIColor.darkGray // Your preferred text color
This ensures the current date’s text stays consistent, regardless of which date is selected.
3. Changing the light blue circle of the selected date
For the selected date’s circle background, use the selectedDayCircleColor property:
calendarAppearance.selectedDayCircleColor = UIColor.purple // Your custom selected circle color
You can also adjust the selected date’s text color with selectedDayTextColor if needed:
calendarAppearance.selectedDayTextColor = UIColor.white
If appearance properties aren’t working (common with heavy customizations)
If you’ve already built a lot of custom views or delegate logic, you might need to explicitly return your custom appearance in the calendar’s delegate method:
func presentationMode() -> CalendarMode { return .monthView } func dayViewAppearance() -> CVCalendarAppearance? { return calendarAppearance // Return your configured appearance here }
Alternatively, if you’re using a custom CVDayView subclass, override the draw(_:) method to manually render the present day and selected day styles exactly how you want. For example:
class CustomDayView: CVDayView { override func draw(_ rect: CGRect) { super.draw(rect) // Draw present day circle with custom color if self.isPresentDay { let circlePath = UIBezierPath(ovalIn: rect.insetBy(dx: 4, dy: 4)) UIColor.yourCustomPresentColor.setFill() circlePath.fill() } // Draw selected day circle with custom color if self.isSelected { let circlePath = UIBezierPath(ovalIn: rect.insetBy(dx: 4, dy: 4)) UIColor.yourCustomSelectedColor.setFill() circlePath.fill() } } }
Then return this custom day view in your delegate:
func dayView(by date: Date, status: DayStatus, indexPath: IndexPath) -> CVDayView { let dayView = CustomDayView() // Configure other day view properties here return dayView }
Give these steps a try—they should cover all the color issues you mentioned. If you’re still stuck, share a snippet of your existing appearance configuration code, and I can help troubleshoot further!
内容的提问来源于stack exchange,提问作者Don Oleksa

