Search code examples
c#xamarinxamarin.formsxamarin.ioscustom-renderer

How to get the TextColor value of a TableSection in my ViewModel Renderer in Xamarin


I am using the code below to make the Text in the TableSections of my CustomTableView Blue (DodgerBlue to be specific) and also make the Text Bold.

assembly: ExportRenderer(typeof(CustomTableView), typeof(CustomTableViewRenderer))]
namespace MyApp.iOS.CustomRenderers
{
    public class CustomTableViewRenderer : TableViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
        {
            base.OnElementChanged(e);
            if (Control == null)
                return;

            var tableView = Control as UITableView;
            var customTableView = Element as CustomTableView;
            tableView.WeakDelegate = new CustomHeaderTableModelRenderer(customTableView);
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
        }


        private class CustomHeaderTableModelRenderer : UnEvenTableViewModelRenderer
        {
            private readonly CustomTableView _customTableView;
            public CustomHeaderTableModelRenderer(TableView model) : base(model)
            {
                _customTableView = model as CustomTableView;
            }

            public override UIView GetViewForHeader(UITableView tableView, nint section)
            {
                return new UILabel()
                {   
                    Text = TitleForHeader(tableView, section),
                    TextColor = UIColor.FromRGB(30, 114, 255), //Issue is here
                    Font = UIFont.BoldSystemFontOfSize(new nfloat(22.0)),
                };
            }
        }
    }
}

The problem I have is that instead of setting the TextColor for the text in the Header with RGB values and forcing it to always be DodgerBlue, I would like to get the color from the TextColor property of each TableSection and use that instead (this will allow me to reuse this CustomTableView elsewhere since I can change the color as I wish). Any idea on how I can do this would be really appreciated


Solution

  • Was able to do this using the code below:

    private class CustomHeaderTableModelRenderer : UnEvenTableViewModelRenderer
    {
        private readonly CustomTableView _customTableView;
        public CustomHeaderTableModelRenderer(TableView model) : base(model)
        {
            _customTableView = model as CustomTableView;
        }
        
        public override UIView GetViewForHeader(UITableView tableView, nint section)
        {
            var tableSectionColor = View.Model.GetSectionTextColor((int) section); // This line gets the Color of the Text in the TitleSection.
            if (tableSectionColor.IsDefault)
            {
                return new UILabel()
                {   
                    Text = TitleForHeader(tableView, section),
                    Font = UIFont.BoldSystemFontOfSize(new nfloat(22.0)),
                };
            }
    
            return new UILabel()
            {   
                Text = TitleForHeader(tableView, section),
                TextColor = tableSectionColor.ToUIColor(), //Use the color here.
                Font = UIFont.BoldSystemFontOfSize(new nfloat(22.0)),
            };
    
        }
    }