Hi Xamarin iOS devs,
today I stumbled upon a problem when using UIFont
. It feels to me like a bug, however I'm not entirely sure what is going on.
I'm using MonoTouch.Dialog to create an entry dialog. Multiline entry is created with SimpleMultilineEntryElement
from the MonoTouch.Dialog ElementPack.
The user visible problem is that sometimes the font used in a multiline entry is smaller than usual.
Corret size:
Wrong size:
The problem affects newly created multiline entries. That is the font size does not change for a given multiline entry, but when I switch to a new view (controller) with a new multiline entry.
The problem is more likely to occur for multiline entries which are not visible initially (on the bottom of the view).
When looking into the problem, I noticed that sometimes the UIFont
used in SimpleMultilineEntryElement
becomes corrupted:
UIFont
when font size is ok:{.HelveticaNeueUI 17}
UIFont
when font size is too small:{ 0}
In the debugger the UIFont
looks somehow broken.
What's confusing about it is that in the C# code, the UIFont is only ever assigned once. I verified that the only assignment is on creation of the multiline element. The next usage is read access to set the font in the text entry.
The relevant code from SimpleMultilineTextEntry is: ....
private UIFont inputFont = UIFont.SystemFontOfSize(17);
....
public override UITableViewCell GetCell(UITableView tv)
{
....
entry = CreateTextField(new RectangleF(size.Width, yOffset, width, size.Height + (height - 44)));
entry.Font = inputFont; // <-- this is where the font is corrupted
The font is initialized on creation of the element. Used in GetCell, where it is already corrupted.
Changing the code to this fixed the problem for me:
....
private UIFont inputFont;
....
public override UITableViewCell GetCell(UITableView tv)
{
....
entry = CreateTextField(new RectangleF(size.Width, yOffset, width, size.Height + (height - 44)));
entry.Font = inputFont ?? UIFont.SystemFontOfSize(17);
Now I am wondering what is happening here. Since UIFont actually lives on the native side, could this problem be related to the mapping between the mono and the native world?
Any insight into what's happening here would be much appreciated :)
Thanks, Stephan