Custom Accessibility Label for iOS calendar
Environment
Product Version | 2021.1.325.1 |
Product | Calendar for Xamarin Cross-Platform |
Description
This article will show you how to access the default accessibility label on iOS calendar for MonthView
Solution
- Create a custom renderer and create custom delegate:
public class CustomCalendarRenderer : CalendarRenderer
{
protected override CalendarDelegate CreateCalendarDelegateOverride()
{
return new CustomCalendarDelegate();
}
}
- The
CustomCalendarDelegate
implementation. You will need to create a custom cell:
public class CustomCalendarDelegate : CalendarDelegate
{
public override TKCalendarCell ViewForCellOfKind(TKCalendar calendar, TKCalendarCellType cellType)
{
if (cellType == TKCalendarCellType.Day)
{
CustomCell cell = new CustomCell();
return cell;
}
return base.ViewForCellOfKind(calendar, cellType);
}
}
- CustomCell implementation. override the AttachWithCalendar method inside the CustomCell, call the base, then implement custom logic to get the information from the events. Then set this information to the AccessibilityLabel.
public class CustomCell : TKCalendarDayCell
{
public override void AttachWithCalendar(TKCalendar owner, NSDate date)
{
base.AttachWithCalendar(owner, date);
// Here you can override the default AccessibilityLabel
if (this.Events != null)
{
StringBuilder builder = new StringBuilder();
foreach (var app in this.Events)
{
builder.AppendLine(string.Format("{0} {1}", app.GetTitle(), app.GetEventColor().ToString()));
}
this.AccessibilityLabel = builder.ToString();
}
}
}