There's nothing in the docs on this, is it possible to center text in a TableView?
Forum Archive
Center text in table
@techteej, just an idea. You can create your own DataSource class with method tableview_cell_for_row. There you'd take tableview's width and then add spaces to the cell's text until it is near the center. It is a hack, I know, but I have no other ideas.
Good luck!
A ui.TableView has ui.TableViewCells. The ui.TableViewCell has a text_label attribute (which really is just a ui.Label), and a label has the alignment property.
In case you should need to do any other kind of unusual formatting in cells, you can also create ui.Views and add them as subviews of the cell's content_view, which is a dynamic view that is automatically resized to the size of the cell when e. g. the delete circle is shown.
@Sebastian, afaik TableViewCell.text_label is a readonly attribute
Example of centered text in ui.TableView:
import ui
class Data (ui.ListDataSource):
def __init__(self, items=None):
ui.ListDataSource.__init__(self, items)
def tableview_cell_for_row(self, tableview, section, row):
cell = ui.TableViewCell()
cell.text_label.text = str(self.items[row])
cell.text_label.alignment = ui.ALIGN_CENTER
return cell
v = ui.TableView()
v.frame = (0, 0, 512, 512)
v.data_source = Data('abcde')
v.delegate = v.data_source
v.present('sheet')
@Sebastian Oh nice, I didn't expect this to work, but it looks like it does! :)
@Sebastian, okay I was wrong it works :)
How would I go about implementing it here? The data source is a one-liner.
class HighScoreView(ui.View):
def __init__(self, high_scores=high_scores):
self.name = 'Cloud Jump 2 - Leaderboard'
tv = ui.TableView()
tv.flex = 'WH'
tv.data_source = ui.ListDataSource(items=self.scores_list(high_scores))
tv.allows_selection = tv.data_source.delete_enabled = False
self.add_subview(tv)
label = ui.ButtonItem()
label.title = '(Local)'
self.right_button_items = [label]
#self.wait_modal()
@classmethod
def scores_list(cls, high_scores):
scores_sorted = sorted(zip(high_scores.values(),
high_scores.keys()), reverse=True)
return ['{:7>} | {}'.format(s, n) for s, n in scores_sorted]
import ui
class Data (ui.ListDataSource):
def __init__(self, items=None):
ui.ListDataSource.__init__(self, items)
def tableview_cell_for_row(self, tableview, section, row):
cell = ui.TableViewCell()
cell.text_label.text = str(self.items[row])
cell.text_label.alignment = ui.ALIGN_CENTER
return cell
high_scores = {'Homer':10, 'Bart':13, 'Lisa':16}
class HighScoreView(ui.View):
def __init__(self, high_scores=high_scores):
self.name = 'Cloud Jump 2 - Leaderboard'
tv = ui.TableView()
tv.flex = 'WH'
tv.data_source = Data(items=self.scores_list(high_scores))
tv.allows_selection = tv.data_source.delete_enabled = False
self.add_subview(tv)
label = ui.ButtonItem()
label.title = '(Local)'
self.right_button_items = [label]
#self.wait_modal()
@classmethod
def scores_list(cls, high_scores):
scores_sorted = sorted(zip(high_scores.values(),
high_scores.keys()), reverse=True)
return ['{:7>} | {}'.format(s, n) for s, n in scores_sorted]
v = HighScoreView()
v.present('sheet')
Thanks. Works great. Only thing now is getting rid of how it looks pushed slightly to the right.