4

Is there a way to set the fonts when a Monotouch Dialog class has been instansiated?

[Section("This is the header")]

This will render with the default blue text with drop shadow, but I can't find where that font is being set. Is there a way to overwrite which font and color it uses?

peirix
  • 36,512
  • 23
  • 96
  • 126

2 Answers2

6

I found a solution for those looking to replace ALL section headers in the entire solution. In MonoTouch.Dialog, there is a class named DialogViewController which is used when creating views with the reflection API. And in here, there's a method called GetViewForHeader(). Instead of sending back just the normal section.HeaderView, you can create a custom label and send that back.

public override UIView GetViewForHeader (UITableView tableView, int sectionIdx)
{
    var section = Root.Sections [sectionIdx];
    if (!string.IsNullOrEmpty(section.Caption))
    {
        var label = new UILabel();
        label.BackgroundColor = UIColor.FromRGB(89, 41, 17);
        label.TextColor = UIColor.FromRGB(255, 206, 52);
        label.ShadowColor = UIColor.Black;
        label.ShadowOffset = new SizeF(0, 1f);
        label.Font = UIFont.FromName("TitlingGothicFB Cond", 20);

        label.Text = section.Caption;

        return label;
    }
    return section.HeaderView;
}

public override float GetHeightForHeader (UITableView tableView, int sectionIdx)
{
    if (!string.IsNullOrEmpty(section.Caption))
        return 40f;
    return -1;
}

Remember to set the height, either manually or by getting the height from the label. You can also create a custom UIView, but a label was sufficient for me.

peirix
  • 36,512
  • 23
  • 96
  • 126
0

When you use the Section like that you will use the UITableView standard rendering.

The only way to change that is to use the Element API instead of the reflection API, and provide a UIView where you draw the contents of the data yourself.

miguel.de.icaza
  • 32,654
  • 6
  • 58
  • 76
  • auch. I already have quite a few reflection classes with several headers. They all need the same font and color, though. Is there really no way to change the header font and color, even if I want them all to be the same? – peirix Feb 09 '11 at 10:28
  • You can modify the source code, and make it so that the constructor that takes strings actually creates views and passes that to the other constructor. – miguel.de.icaza Feb 09 '11 at 22:44