0

I am using padding in my UITableViewSource to format a cell. I have:

//cell declared somewhere in `UITableViewSource`

var name = rows[indexPath.Row].MachineName.PadRight(50);

I have tried this as well:

var name = rows[indexPath.Row].MachineName.PadRight(50, ' ');

I have some set the cell text:

cell.TextLabel.Text = "01234567" + " " + name + "01234567";

The output looks like:

01234567 SomeNameHere           01234567
-------------------------------------------
01234567 SomeNameHere           01234567
--------------------------------------------
01234567 SomeOtherNameHere         01234567
--------------------------------------------
01234567 ShortName            01234567
---------------------------------------------

I have checked the length of name and all of them are 50 but when its shown on my device in the cell it doesn't line up. Is there something special with Xamarin, iOS, UITableViewCell - something that may be different.

My work around for this would be to create a UITableViewCell using XCode but it would be nice if this could work.

jharr100
  • 1,449
  • 24
  • 51
  • 1
    By default, it is not using a Monospaced font. – Jason Mar 24 '14 at 16:26
  • One solution would be to use separate labels for each column – Martin R Mar 24 '14 at 16:28
  • @MartinR Thats what I was thinking I would do if I couldn't get around this another way – jharr100 Mar 24 '14 at 16:31
  • 1
    Yes, use a monospaced font. However, if you are trying to align data in columns, using a custom cell with individual labels is probably a better approach. You could also try the tab spacing trick that Lawrence suggests. – Jason Mar 24 '14 at 16:36
  • 1
    @jharr100: I just found this: http://stackoverflow.com/a/18926262/1187415 . You can create an "attributed string" with tab stops. – Martin R Mar 24 '14 at 16:44

2 Answers2

1

Try:

var name = rows[indexPath.Row].MachineName.PadRight(10, '\t');
bosnjak
  • 8,424
  • 2
  • 21
  • 47
0

I ended up using a monospaced font as suggested by @Jason and it seems Courier New is the only one available in iOS just from my quick search.

jharr100
  • 1,449
  • 24
  • 51